import java.util.Arrays;
public class test1 {
public static void main(String[] args) {
Student[] stus = new Student[3];
stus[0] = new Student("张三", 23);
stus[1] = new Student("李四", 24);
stus[2] = new Student("王五", 25);
delete(stus);
System.out.println(Arrays.toString(stus));
}
private static void delete(Student[] stus) {
// 1,增强for循环
for (Student student : stus) {
if ("王五".equals(student.name)) {
student = null;
break;
}
}
//,2,普通for循环
for (int i = 0; i < stus.length; i ++){
if ("王五".equals(stus[i].name)) {
stus[i] = null;
break;
}
}
}
}
class Student {
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return name;
}
public int getAge() {
return age;
}
} |
|