String s = "hello";
String t="hello";
char[] c = {'h','e','l','l','o'};
System.out.println( s.euqals( t ) );
System.out.println( t.equals( c ) );
System.out.println( s==t );
System.out.println( t.equals( new String(c) ) );
System.out.println( t.equals( new String("hello")) );
String 类是 final 类故不可以继承。 空间不可变吧
String 类是 final 类故可以继承。
String 类不是 final 类故不可以继承。
String 类不是 final 类故可以继承。
A. false B. true C. 1 D. 0
A.”bcde” B.”cde” C.”cdef” D.”def”
A.a B.b C.f D.g
① 属性:身份证号,姓名,性别,年龄,户籍,出生日期(String类型,"1998-09-09")
② 属性私有化,提供get/set,构造方法
③ 介绍格式:(toString)
姓名+户籍+年龄
④ 提供对象比较equals方法,只要身份证号+姓名相同就认为对象相等
⑤ 写一个测试类,定义一个Person数组,创建多个Person对象存储在数组中,
i. 打印输出所有Person对象的信息
ii. 用控制台输入一个Person信息:(注意:跟昨天的输入有区别!!!)
例如:"370828199902145066-刘航-男-58-北京-1955/01/01"
判断数组中是否存在和此对象相等的对象(身份证号+姓名相同就认为对象相等)
iii. 判断Person数组中,名字是否有包含"胡"的联系人,有,打印信息.
iv. 任意输入一个姓 ,判断数组中是否存在有此姓的Person对象.
v.输出5月份出生的学生信息
package com.by.homework2;
public class Person{
private String idCard;
private String name;
private String sex;
private int age;
private String location;
private String birthday;
public Person() {
}
public Person(String idCard, String name, String sex, int age, String location, String birthday) {
this.idCard = idCard;
this.name = name;
this.sex = sex;
this.age = age;
this.location = location;
this.birthday = birthday;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
/* public String toString(){
return "姓名:"+name+",户籍:"+location+",年龄:"+age;
}*/
@Override
public String toString() {
return "姓名:"+name+",户籍:"+location+",年龄:"+age;
}
public boolean equals(Object o){
if(this==o){
return true;
}
if (this.getClass()!=o.getClass()||o.getClass()==null){
return false;
}
Person person=(Person) o;
return this.idCard.equals(person.idCard)&&this.name.equals(person.name);
}
}
package com.by.homework2;
import java.util.Scanner;
public class PersonTest {
public static void main(String[] args) {
Person[]people={new Person("1307262000","张三","男",18,"河北省","2000-01-01"),
new Person("1307262001","李四","女",19,"河南省","2000-05-01"),
new Person("1307262002","王五","男",20,"湖北省","2000-03-01"),
new Person("123456789","胡麻子","男",21,"湖南","2000-11-11")};
Scanner sc=new Scanner(System.in);
for (int i = 0; i < people.length; i++) {
System.out.println(people[i]);
}
//查询胡姓人
boolean boo2=false;
for (int i = 0; i < people.length; i++) {
if(people[i].getName().indexOf("胡")==0){
System.out.println("有胡姓叫:"+people[i].getName());
boo2=true;
}
}
if(!boo2){
System.out.println("没有胡姓人");
}
char[]chars;
for (int i = 0; i < people.length; i++) {
chars=people[i].getName().toCharArray();
}
/*for (int i = 0; i < chars.length; i++) {
chars[0]
}*/
//查询是否有该姓的名字
System.out.println("任意输入一个姓,看是否有");
String name1=sc.next();
char []chars1=name1.toCharArray();
String str3= String.valueOf(chars1[0]);
boolean boo3=false;
for (int i=0;i< people.length;i++){
boolean boo4=people[i].getName().contains(str3);
if (boo4){
System.out.println("有姓名为::"+people[i]);
boo3=true;
}
}
if (!boo3){
System.out.println("无相等的");
}
for (int i = 0; i < people.length; i++) {
if(people[i].getBirthday().indexOf("5")==6){
System.out.println(people[i]+",生日为:"+people[i].getBirthday());
}
}
for (int i = 0; i < people.length; i++) {
String str=String.valueOf(people[i].getBirthday().charAt(6));
if(str.equals("5")){
System.out.println(people[i]+",生日为:"+people[i].getBirthday());
}
}
System.out.println("请输入身份证号");
String idCard=sc.next();
System.out.println("请输入姓名");
String name=sc.next();
System.out.println("请输入性别");
String sex=sc.next();
System.out.println("请输入年龄");
int age=sc.nextInt();
System.out.println("请输入籍贯");
String location=sc.next();
System.out.println("请输入出生日期");
String birthday=sc.next();
Person person=new Person(idCard,name,sex,age,location,birthday);
String str=idCard+"-"+name+"-"+sex+"-"+age+"-"+location+"-"+birthday;
System.out.println(str);
String []str1=str.split("-");
boolean boo1=false;
for (int i = 0; i < people.length; i++) {
if (str1[0].equals(people[i].getIdCard())&&str1[1].equals(people[i].getName())){
System.out.println("相等");
boo1=true;
}
}
if (!boo1){
System.out.println("不相等");
}
//输入一个完整的信息
System.out.println("请输入信息");
String infor=sc.next();
String []strings=infor.split("-");
Person person1=new Person(strings[0],strings[1],strings[2],Integer.parseInt(strings[3]),strings[4],strings[5]);
boolean boo=false;
for (int i = 0; i < people.length; i++) {
if(person1.equals(people[i])){
System.out.println("相等的为:"+people[i]);
boo=true;
}
}
if(!boo){
System.out.println("没有相等的");
}
}
}
String s3="[email protected]";
System.out.println(s3.substring(0,6));
合法邮箱要求:
1、有“@”和“.”符号
2、“.”在“@”后面
package com.by.homework2;
public class ContainAndIndexOf {
public static void main(String[] args) {
String str="[email protected]";
if (str.contains("@")&&str.contains(".")){
if (str.indexOf(".")>str.indexOf("@")){
System.out.println(str+":是一个合法邮箱");
}
}
}
}
package com.by.homework2;
import java.util.Scanner;
public class Worker {
private String name;
private int age;
private String sex;
private double salary;
public Worker() {
}
public Worker(String name, int age, String sex, double salary) {
this.name = name;
this.age = age;
this.sex = sex;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Worker{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", salary=" + salary +
'}';
}
}
class WorkerTest{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入员工信息");
String str=sc.next();
String []strings=str.split("-");
String name = null;
int age=0;
String sex=null;
double salary = 0;
Worker worker1 = null;
Worker worker2 = null;
Worker worker3 = null;
Worker worker4 = null;
Worker worker5 = null;
int num=0;
//zhang-18-nan-1000-li-19-nan-2000-wang-20-nan-3000-zhao-21-nan-4000-qian-22-nan-5000
for (int i=1;i<=5;i++) {
name=strings[num];
age=Integer.parseInt(strings[1+num]);
sex=strings[2+num];
salary=Double.parseDouble(strings[3+num]);
System.out.println(name+age+sex+salary);
if (i==1){
worker1=new Worker(name,age,sex,salary);
}
if (i==2){
worker2=new Worker(name,age,sex,salary);
}
if (i==3){
worker3=new Worker(name,age,sex,salary);
}
if (i==4){
worker4=new Worker(name,age,sex,salary);
}
if (i==5){
worker5=new Worker(name,age,sex,salary);
}
// System.out.println();
num+=4;
}
Worker[] workers={worker1,worker2,worker3,worker4,worker5};
for (int i = 0; i < workers.length; i++) {
System.out.println(workers[i].toString());
}
}
}
请输入员工信息
zhang-18-nan-1000-li-19-nan-2000-wang-20-nan-3000-zhao-21-nan-4000-qian-22-nan-5000
zhang18nan1000.0
li19nan2000.0
wang20nan3000.0
zhao21nan4000.0
qian22nan5000.0
Worker{name='zhang', age=18, sex='nan', salary=1000.0}
Worker{name='li', age=19, sex='nan', salary=2000.0}
Worker{name='wang', age=20, sex='nan', salary=3000.0}
Worker{name='zhao', age=21, sex='nan', salary=4000.0}
Worker{name='qian', age=22, sex='nan', salary=5000.0}
Process finished with exit code 0
.chatAt(17)!=0 && .charAt(17+) ==0
如:“376472364760183013010832“;
统计每个数字出现的次数
String s="376472364760183013010832";
byte[] s1=s.getBytes();
int sum=0;
for (int i = 0; i < s1.length; i++) {
for (int j = 0; j <= s1.length-1; j++) {
if(s1[i]==s1[i+1]){
sum++;
}
}
}
System.out.println(s1[i]+"出现的次数是"+sum);
}
提示:java.util.UUID.randomUUID().toString()//可以获取随机UUID
格式:e6c57443-1667-4d75-98f6-a8863d95e58f
package com.by.homework2;
import java.util.UUID;
public class Uuid {
public static void main(String[] args) {
String uuid=UUID.randomUUID().toString();
System.out.println(uuid);
String []str=uuid.split("-");
for (int i = 0; i < str.length; i++) {
System.out.print(str[i]);
}
}
}
提示:创建随机数对象java.util.Random.
java.util.Random r=new java.util.Random();
random.next(100);//可以获取到0-99中任意一个随机数。
获取到的随机数作为字符串的字符下标进行取值。
package com.by.homework2;
import java.util.Random;
public class RandomAppends {
public static void main(String[] args) {
String input = "ABDEFGabdefghijklmnMNKJ1234567";
Random random = new Random();
StringBuilder sb = new StringBuilder();
while (sb.length() < 4) {
int index = random.nextInt(input.length());
char c = input.charAt(index);
String str=String.valueOf(c);
if (!sb.toString().contains(str)) {
sb.append(c);
}
}
System.out.println(sb.toString());
}
}
package com.by.homework2;
import java.util.Scanner;
public class Roolback {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入字符串");
String str=scanner.next();
System.out.println("反转其中内容");
String str1=scanner.next();
int num=str.indexOf(str1);
char []chars=str.toCharArray();
char a=0;
char b=0;
for (int i = 0; i < chars.length; i++) {
if (i==num){
a=chars[i];
b=chars[i+1];
chars[i]=chars[i+str1.length()-1];
chars[i+1]=chars[i+str1.length()-2];
chars[i+str1.length()-1]=a;
chars[i+str1.length()-2]=b;
}
System.out.print(chars[i]);
}
/* String a="abcdefg";
String b="edcb";
char[]cs=b.toCharArray();
String c="";
for (int i = cs.length-1; i >=0; i--) {
c+=cs[i];
}
System.out.println(a.replace(b,c));*/
}
}
请输入字符串
abcdefg
反转其中内容
cdef
abfedcg
Process finished with exit code 0
package com.by.homework2;
public class Test15 {
public static void main(String[] args) {
String str="zxcvbnmalskqpwoeiruty156789~!@#$%^&*()";
String str1="!^#*145agtkdbm";
char []c=str.toCharArray();
char []c1=str1.toCharArray();
int num=0;
for (int i = 0; i < c.length; i++) {
for (int i1 = 0; i1 < c1.length; i1++) {
if (String.valueOf(c[i]).equals(String.valueOf(c1[i1]))){
num++;
}
}
}
System.out.println(num);
}
}
"这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";
package com.by.homework2;
public class Test16 {
public static void main(String[] args) {
String str = "这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";
int count = 0;
int index = 0;
while ((index = str.indexOf("朋友", index)) != -1) {
count++;
index++; // Move the index to the next character after "朋友"
}
System.out.println("朋友出现的次数: " + count);
}
}
实现:让用户随机输入一段话,如果出现上述的敏感词汇,则用“***”对其进行替换
package com.by.homework2;
import java.util.Scanner;
public class Test17 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一段话,我来输出");
String str=sc.next();
String []strings={"性","色情","爆炸","恐怖","枪","军火"};
String str2="****";
for (int i = 0; i < strings.length; i++) {
if (str.contains(strings[i])){
str.replace(strings[i],str2);
System.out.println(str);
}
}
package com.by.homework2;
import java.util.Scanner;
public class Test18 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入:");
String str=sc.next();
String str1;
String str2;
int num=0;
byte[]bytes=str.getBytes();
char []chars=new char[bytes.length];
for (int i = 0; i < bytes.length; i++) {
if(bytes[i]>=65&&bytes[i]<=90){
chars[i]= (char) bytes[i];
num++;
}
if(bytes[i]>=97&&bytes[i]<=122){
chars[i+num]= (char) bytes[i];
}
}
for (int j = 0; j < chars.length; j++) {
System.out.print(chars[j]);
}
}
}