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());
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57