arrayList
arrayList
arrayList为空时,容量为0;
添加元素后,默认容量DEFAULT__CAPACITY= 10;
当超过10后,每次扩容大小为原来的1.5倍

- 增加 .add()
- 删除 .remove()
- 遍历 for或者iterator或者ListIterator
 .next()
 .previous()
- 判断 .contains()
 .isEmpty()
- 获取下标 .indexOf()

import oop.Demo04.Student;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class Demo02 {
    public static void main(String[] args) {
        //创建集合
        ArrayList arrayList =new ArrayList<>();
        //1.添加元素
        Student s1=new Student("刘",10);
        Student s2=new Student("关",20);
        Student s3=new Student("张",30);
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        arrayList.add(s3);
        arrayList.add(s1);
        System.out.println("元素个数:"+arrayList.size());
        System.out.println(arrayList.toString());
        //2.删除元素
        arrayList.remove(s1);
        System.out.println("删除后元素个数:"+arrayList.size());
        System.out.println(arrayList.toString());
        //3.遍历元素
        System.out.println("------------迭代器----------------------");
        Iterator iterator = arrayList.iterator();
        while(iterator.hasNext()){
            Student s = (Student) iterator.next();
            System.out.println(s.toString());
        }
        System.out.println("------------list迭代器----------------------");
        ListIterator listIterator = arrayList.listIterator();
        while (listIterator.hasNext()){
            Object next = listIterator.next();
            System.out.println(next);
        }
        System.out.println("------------list迭代器逆序----------------------");
        while (listIterator.hasPrevious()){
            Object previous = listIterator.previous();
            System.out.println(previous);
        }
        //4.判断
        System.out.println(arrayList.isEmpty());
        System.out.println(arrayList.contains(s1));
        //查找
        System.out.println(arrayList.indexOf(s1));
    }
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号