请教一下,
1.为什么这里arr.length,会导致else里面的内容无法运行?
2.为什么我定义的构造方法getIndex(),通过在main方法里面调用之后会打印的是地址值,而不是数组里面存储的学生对象?
package com.itheima.aritheticoperator;
public class Test2 {
/*创建一个长度为3的数组,存储1-3个学生对象作为初始化数据
学生对象的姓名,学号各不相同。学生属性:学号,姓名,年龄
1.再添加一个学生对象,并对一个学生对象进行唯一性判断
2.添加完毕之后,遍历所有学生信息
3.通过id删除学生对象,如果存在则删除,如果不存在则显示删除失败
4.删除完毕之后,遍历所有学生信息
5.查询数组id为“heima002”,如果存在,则年龄加一
* */
public static void main(String[] args) {
Student[] arr = new Student[3];
Student stu = new Student("张三", 1, 25);
Student stu1 = new Student("李四", 2, 26);
Student stu2 = new Student("春夜", 3, 27);
arr[0] = stu;
arr[1] = stu1;
arr[2] = stu2;
Student stu3 = new Student("赵虎", 4, 28);
boolean falg=contains(arr,4);
if(falg){
System.out.println("当前id不存在");
}else{
int count=getcount(arr);
if (count== arr.length){
Student[] stu5=createNewArr(arr);
stu5[count]=stu3;
getIndex(stu5);
}else{
arr[count]=stu3;
getIndex(arr);
}
}
}
public static void getIndex(Student[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static Student[] createNewArr(Student[] arr){
Student[] newArr= new Student[arr.length+1];
for (int i = 0; i < arr.length; i++) {
newArr[i]=arr[i];
}
return newArr;
}
public static int getcount(Student[] arr){
int count=0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]!=null){
count++;
}
}
return count;
}
public static boolean contains(Student[] arr, int id) {
for (int i = 0; i < arr.length; i++) {
Student stu=arr[i];
int sid= stu.getId();
if(sid==id){
return true;
}
}
return false;
}
}