①定义

ArrayList是一个动态数组,也是我们常用的集合,它允许任何元素的插入,甚至包括null。每一个ArrayList都有一个初始化的容量(10),该容量代表了数组的大小,随着容器中容量的不断增加,容器的大小也会随着增加。在每次向容器中增加元素时,会进行容量检查,当快溢出时,会进行扩容操作。

②特性

  •    允许插入的元素重复
  •    插入的元素是有序的
  •    动态扩容
  •    非线程安全,异步
  •    基于动态数组的数据结构
  •    擅长随机访问(get set)

③常用方法

  • add(Object e)
  • add(int index ,Object e)
  • addAll(Collection c) 
  • addAll(int index , Collection c)
  • size()
  • get(int index)
  • set(int index,Object e)
  • indexOf(Object c)
  • lastIndexOf(Object c)
  • isEmpty()
  • remove(int index)
  • remove(Object c)
  • removeAll(Collection<?> c)
  • contains(Object c)
  • containsAll(Collection<?> c)
  • clear()
  • clone()
  • iterator()
  • retainAll(Collection<?> c)
  • subList(int fromIndex,int toIndex)
  • trimToSize()  回收多余容量
  • toArray()
  • toArray(T[] a)

③示例

add/addAll/set/size/get/indexOf/lastIndexOf/isEmpty


    @Test
    public void addTest() {
        ArrayList list = new ArrayList();
        list.add(0);
        list.add(1);
        list.add(2);
        list.add(3); // 此时list : 0 1 2 3
        print("list", list);
        list.add(0, 4); //  在指定位置插入元素
        print("list", list); //  此时list ;4 0 1 2 3
        List newList = list.subList(2, 4); //截取list ,不包含最后一位
        print("newList", newList); //  此时 newList : 1 2
        list.addAll(2, newList); //新增list,新的list元素默认排在原来元素的后面 ,此时newList 不能再使用了
        print("list", list); // 此时,list : 4 0 1 2 1 2 3
        System.out.println("list size " + list.size());
        list.set(0, 1); //在指定位置替换元素
        print("list", list); // 此时 list : 1 0 1 2 1 2 3
        System.out.println(list.indexOf(1));
        System.out.println(list.lastIndexOf(1));
        System.out.println(list.isEmpty());

    }

    private void print(String flag, List list) {
        System.out.print(flag + ":");
        list.forEach(System.out::print);
        System.out.println();
    }

remove/removeAll

  使用removeAll ,需要重写equals()方法

    @Test
    public void removeTest() {
        ArrayList<String> list = new ArrayList<>();
        createDefaultList(list); // list : 0 1 2 3
        print("list", list);
        list.remove(2); // list : 0 1 3
        print("list", list);
        list.add(2, "2");
        print("list", list);
        list.remove("2");
        print("list", list);

        ArrayList<String> newList = new ArrayList<>();
        newList.add("0");
        newList.add("1");
        print("old list", list);
        print("new list", newList);
        list.removeAll(newList);
        print("list", list);

    }

    private void createDefaultList(ArrayList<String> list) {
        list.add("0");
        list.add("1");
        list.add("2");
        list.add("3");
    }

contains/containsAll  重写equals()方法

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 int getAge() {
            return age;
        }

        @Override
        public boolean equals(Object object) {
            Student student = (Student) object;
            return name.equals(student.getName()) && age == student.getAge();
        }
    }

    @Test
    public void containsTest() {
        ArrayList<Student> students = new ArrayList<>();
        Student ryan = new Student("Ryan", 25);
        Student kiven = new Student("Kiven", 25);
        students.add(ryan);
        students.add(kiven);
        Student ry = new Student("Ryan", 25);
        System.out.println(students.contains(ry));
        Student kv = new Student("Kiven", 25);
        ArrayList<Student> sts = new ArrayList<>();
        sts.add(ry);
        sts.add(kv);
        System.out.println(students.containsAll(sts));

    }

subList/clear

  修改sublist会影响原来的list

  public void subListTest() {
        //修改sublist 会影响原来的list
        ArrayList<String> l1 = new ArrayList<>();
        l1.add("a");
        l1.add("b");
        l1.add("c");
        List<String> l2 = l1.subList(1, 2);
        l2.add("new");
        System.out.println(l1);
        System.out.println(l2);

    }

修改原来的list,sublist的所有操作都会报错

 @Test
    public void subListTest() {
        //修改sublist 会影响原来的list
        ArrayList<String> l1 = new ArrayList<>();
        l1.add("a");
        l1.add("b");
        l1.add("c");
        List<String> l2 = l1.subList(1, 2);
        l1.add("new");
        System.out.println(l1);
        System.out.println(l2);

    }

截取原来的list

 @Test
    public void subListTest() {
        //修改sublist 会影响原来的list
        ArrayList<String> l1 = new ArrayList<>();
        l1.add("a");
        l1.add("b");
        l1.add("c");
        l1.subList(1, 2).clear();
        System.out.println(l1);

    }

clone

  @Test
    public void cloneTest() {
        ArrayList<Student> students = new ArrayList<>();
        Student student = new Student("Ryan", 25);
        Student student1 = new Student("Kiven", 24);
        students.add(student);
        students.add(student1);
        ArrayList<Student> students2 = (ArrayList<Student>) students.clone();
        students2.remove(1);
        System.out.println(students);
        System.out.println(students2);

    }

toArray

直接转换报错

    public void arrayTest() {
        ArrayList<String> list = new ArrayList();
        list.add("1");
        list.add("2");
        String[] number = (String[]) list.toArray();
        System.out.println(number);
    }

 @Test
    public void arrayTest() {
        ArrayList<String> list = new ArrayList();
        list.add("1");
        list.add("2");
        Object[] number = list.toArray();
        for (int i = 0; i < number.length; i++) {
            String e = (String) number[i];
            System.out.println(e);
        }
        System.out.println(number);
    }

toArray(T[] a)

@Test
    public void arrayTest() {
        ArrayList<String> list = new ArrayList();
        list.add("1");
        list.add("2");
        String[] number = list.toArray(new String[0]);
        for (int i = 0; i < number.length; i++) {
            System.out.println(number[i]);
        }
    }

iterator

@Test
    public void arrayTest() {
        ArrayList<String> list = new ArrayList();
        list.add("1");
        list.add("2");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String str = iterator.next();
            System.out.println(str);
        }
    }

retainAll  -- 删除不在指定集合中的元素

@Test
    public void retainAllTest() {
        ArrayList<String> list = new ArrayList();
        list.add("1");
        list.add("2");
        list.add("3");
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("3");
        list.retainAll(list1);
        System.out.println(list);

    }

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐