DESKTOP-2IHO1NT_20200831秦文亮
问题
1、调用方法
public static void main(String[] args) {
String s = "abc";
System.out.println(s);
arr(s);
System.out.println(s);
}
public static void arr(String a) {
a = "aaa";
System.out.println(a);
}
已解决
2、冒泡交换位置时变量的类型
public static void main(String[] args) {
Phoenix[] phoenixs = new Phoenix[ 5 ];
phoenixs[ 0 ] = new Phoenix( "凤凰卫士" , 150 , '男' );
phoenixs[ 1 ] = new Phoenix( "凤凰大侠" , 200 , '男' );
phoenixs[ 2 ] = new Phoenix( "小凤凰瞎" , 250 , '男' );
phoenixs[ 3 ] = new Phoenix( "大凤凰黑" , 750 , '男' );
phoenixs[ 4 ] = new Phoenix( "凤凰天使" , 100 , '女' );
for(int i=0;i<phoenixs.length-1;i++) {
for(int j=0;j<phoenixs.length-1-i;j++) {
if(phoenixs[j].getAge()>phoenixs[j+1].getAge()) {
Phoenix s=phoenixs[j];
phoenixs[j]=phoenixs[j+1];
phoenixs[j+1]=s;
}
}
}for(int i=0;i<phoenixs.length;i++) {
System.out.println(phoenixs[i]);
}
}
交换位置是定义了一个Phoenix类型的变量用来交换位置;不能用int类型的变量来接收。
有String类型的数组可以拿char类型的变量来接受吗?
3、substring的使用
String a="hello";
String b=a.substring(0, 2);
System.out.println(b);
输出结果为he
返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。
返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex
处开始,直到索引 endIndex - 1
处的字符。因此,该子字符串的长度为 endIndex-beginIndex
。
已解决
4、equals的使用
public class Student {
String name;
int age;
public Student(String name,int age) {
this.name =name;
this.age=age;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public static void main(String[] args) {
Student s1=new Student("nana",10);
Student s2=new Student("nana",10);
System.out.println(s1.equals(s2));
}
}
equals方法的比较时必须先重写equals(Object)方法
对重写equals(Object)方法不会写,没有理解
Arrays.equals方法不能比较含有String类型的俩个数组吗?
吐槽
又是忙碌的一天!
阿佟每天给我讲题很认真!!
加油!!!
近期评论