System.out.println(“你要添加在第几个?----”);
// int index = new Scanner(System.in).nextInt();
//
// System.out.println(“输入添加的学生姓名:”);
// String name = new Scanner(System.in).next();
// System.out.println(“输入添加的学生年龄:”);
// int age = new Scanner(System.in).nextInt();
//
// StudentBean[] newStuArr = new StudentBean[stuArr.length+1];
// //法1:
for(int i = 0;i<index -1;i++) {
newStuArr[i] = stuArr[i];
}
newStuArr[index-1] = new StudentBean(name, age);
for(int i = index ; i<newStuArr.length;i++) {
newStuArr[i] = stuArr[i-1];
}
//
// //法2:
// System.arraycopy(stuArr, 0, newStuArr, 0, index -1);
// newStuArr[index-1] = new StudentBean(name, age);
// System.arraycopy(stuArr, index-1, newStuArr, index, newStuArr.length-index);
//
// stuArr = newStuArr;
// for (int i = 0; i < stuArr.length; i++) {
// System.out.println(stuArr[i].getName()+ " " +stuArr[i].getAge());
// }
//
//
用for循环和arraycopy在引用数据类型数组中添加对象
最新推荐文章于 2021-05-25 17:50:28 发布