public static void main(String[] args) {
Student[] stuArr = new Student[20];
String[] name1 = {"赵","钱","孙","李","周","吴","郑","王"};
String[] name2 = {"明","阳","微","男","楠","博","三","歌"};
// 遍历才可以用增强for
for(int i = 0; i < stuArr.length; i++) {
int id = i + 1;
int index1 = (int)(Math.random() * 100) % name1.length;
int index2 = (int)(Math.random() * 100) % name2.length;
String name = name1[index1] + name2[index2];
int grade = (int)(Math.random() * 100) % 6 + 1;
double score = (int)(Math.random() * 101);
stuArr[i] = new Student(id,name,grade,score);
}
// 增强for 数据类型 变量名 : 数组名
for(Student temp : stuArr) {
System.out.println(temp.say());
}
System.out.println("########################@@@@@@@@@@@@@@@@@@@##############");
// 把学生对象也反转
for(int i = 0; i < stuArr.length / 2; i++) {
Student temp = stuArr[i];
stuArr[i] = stuArr[stuArr.length - i - 1];
stuArr[stuArr.length - i - 1] = temp;
}
// 增强for 数据类型 变量名 : 数组名
for(Student temp : stuArr) {
System.out.println(temp.say());
}
}
java基础[反转对象数组]
最新推荐文章于 2022-04-26 20:03:43 发布