4、ArrayList的使用
package ArrayList01;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class day05 {
public static void main(String[] args) {
ArrayList arrayList =new ArrayList<>();
Student s1= new Student("张三",18);
Student s2= new Student("李四",20);
Student s3= new Student("王五",25);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
arrayList.remove(s1);
arrayList.remove(new Student("李四",20));
System.out.println("删除后的元素"+arrayList.size());
System.out.println(arrayList.toString());
System.out.println("===============for循环===============");
for(int i=0;i<arrayList.size();i++){
System.out.println(arrayList.get(i));
}
System.out.println("============增强for循环===============");
for(Object obj:arrayList){
System.out.println(obj);
}
System.out.println("============使用迭代器===============");
Iterator it=arrayList.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println("============使用列表迭代器===============");
ListIterator it1=arrayList.listIterator();
while (it1.hasNext()){
System.out.println(it1.next());
}
}
}