workplace-blog

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ArrayList

1.ArrayList特点:

  • 数组结构实现,查询快,增删慢
  • jdk1.2,运行效率快,线程不安全

2.ArrayList使用:

package study01.Collection;
//学生工具类

public class student {
    private String name;
    private int age;

    public student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        student student = (student) o;
        if (this.age == student.age && this.name == student.name) return true;
        return false;
    }

}
/*
ArrayList的使用
 */
public class arrayListDemo01 {
    public static void main(String[] args) {
        //创建ArrayList对象
        ArrayList arrayList = new ArrayList<>();
        student s1 = new student("张三",19);
        student s2 = new student("李四",19);
        student s3 = new student("王五",19);
        student s4 = new student("赵六",22);
        //1.添加
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        arrayList.add(s4);
        System.out.println(arrayList);

        /*
        2.删除
        remove方法底层调用了Object的equals方法,比较的是对象的引用
        arrayList.remove(new student("张三",19));无法删除new出来的新对象
        若想删除需要重写equals方法
         */
        arrayList.remove(s1);
        arrayList.remove(new student("张三",19));//成功删除
        System.out.println("-------2.删除---------");
        System.out.println(arrayList);

        //3.遍历
        //3.1使用迭代器
        Iterator it = arrayList.iterator();
        System.out.println("-------3.1使用迭代器---------");
        while (it.hasNext()){
            student s = (student)it.next();
            System.out.println(s);
        }
        //3.2使用列表迭代器顺序
        ListIterator lit = arrayList.listIterator();
        System.out.println("-------3.1使用列表迭代器顺序--------");
        while (lit.hasNext()){
            student s = (student)lit.next();
            System.out.println(s);
        }
        //3.3使用列表迭代器逆序
        System.out.println("-------3.1使用列表迭代器逆序--------");
        while (lit.hasPrevious()) {
            student s = (student) lit.previous();
            System.out.println(s);
        }

        //4.判断
        System.out.println("---------4.判断--------");
        System.out.println(arrayList.contains(new student("赵六",22)));

        //5.查找
        System.out.println("---------5.查找--------");
        System.out.println(arrayList.indexOf(s2));
    }
}

//输出结果:
[student{name='张三', age=19}, student{name='李四', age=19}, student{name='王五', age=19}, student{name='赵六', age=22}]
-------2.删除---------
[student{name='李四', age=19}, student{name='王五', age=19}, student{name='赵六', age=22}]
-------3.1使用迭代器---------
student{name='李四', age=19}
student{name='王五', age=19}
student{name='赵六', age=22}
-------3.1使用列表迭代器顺序--------
student{name='李四', age=19}
student{name='王五', age=19}
student{name='赵六', age=22}
-------3.1使用列表迭代器逆序--------
student{name='赵六', age=22}
student{name='王五', age=19}
student{name='李四', age=19}
---------4.判断--------
true
---------5.查找--------
0

3.ArrayList源码分析:

  • private static final int DEFAULT_CAPACITY = 10;
    

DEFAULT_CAPACITY = 10: 默认容量为10;

  • private int size;
    

size:默认为0

  • public ArrayList() {   
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }
    

无参构造:创建数组时初始化等于一个空数组(DEFAULTCAPACITY_EMPTY_ELEMENTDATA),size=0,容量=0

  • public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
    
  • private void ensureCapacityInternal(int minCapacity) {
        ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
    }
    
  • private static int calculateCapacity(Object[] elementData, int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            return Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        return minCapacity;//10
    }
    
  • private void ensureExplicitCapacity(int minCapacity) {
        modCount++;
    
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
    
  • private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }
    

add()方法:(Array List每次扩容1.5倍)

  • 初始化容量为0,size为0;
  • 添加一个元素,容量为10,size=1;
  • 元素超过10(当前容量)时,进行扩容
  • 添加第十一个元素,容量为15,size=11;

4.Vector集合

  • 特点:

    • 数组结构实现,查询快,增删慢
    • jdk1.0,运行效率慢,线程安全
  • Vector使用

//Vector使用
public class vectorDemo01 {
    public static void main(String[] args) {
        //创建集合
        Vector vector = new Vector<>();
        //1.增加
        vector.add("夏黑葡萄");
        vector.add("无籽西瓜");
        vector.add("海南青芒");
        vector.add("阳光玫瑰");
        System.out.println(vector);

        //2.删除
        vector.remove("阳光玫瑰");

        //3.遍历,枚举器
        System.out.println("---------枚举器---------");
        Enumeration enm = vector.elements();
        while (enm.hasMoreElements()){
            String s = (String)enm.nextElement();
            System.out.println(s);
        }

        //4.判断
        System.out.println("---------判断---------");
        System.out.println(vector.isEmpty());
        System.out.println(vector.contains("无籽西瓜"));
        //5.其他方法
        System.out.println("---------其他方法---------");
        System.out.println("vector第一个元素为:" + vector.firstElement());
        System.out.println("vector最后一个元素为:" + vector.lastElement());
        System.out.println("vector下标为0的元素为:" + vector.elementAt(0));
    }
}

//输出结果为:
[夏黑葡萄, 无籽西瓜, 海南青芒, 阳光玫瑰]
---------枚举器---------
夏黑葡萄
无籽西瓜
海南青芒
---------判断---------
false
true
---------其他方法---------
vector第一个元素为:夏黑葡萄
vector最后一个元素为:海南青芒
vector下标为0的元素为:夏黑葡萄
posted on 2022-08-26 19:23  多巴胺LLL  阅读(36)  评论(0)    收藏  举报