ArrayList class

ArrayList is a collection of data storage, including what we call the elements can also be called variable array .


ArrayList format

Format:

ArrayList <E> list = new ArrayList<E>();

<E> is used to fill in the paradigm (eight types), and only reference data type can be filled in.

Except for Integer and Character, only the first letter is allowed to be capitalized.


Common method

add : Add elements.
remover : Delete the specified index element and return.
get : Get a single element.
size : Return all the elements of the collection to prevent out of bounds when traversing the collection.

// 创建集合对象
        ArrayList <String> list = new ArrayList<String>();

        // 添加元素
        list.add("hello ");
        list.add(" world");
        list.add("java");

        // 移除指定位置的索引并且去返还被删除的元素
        System.out.print(list.remove(0));
        System.out.println(list.remove(0));
        System.out.println(list.remove(0));
        // 打印list集合元素都被删完了 所以 输出来是 []
        System.out.println(list);
        System.out.println(); // 换行


        // 添加元素
        list.add("hello");
        list.add("world");
        list.add("java");

        // 如果想要拿到 list中单独的元素就要使用get 返还集合中指定的元素
        System.out.println(list.get(0));
        System.out.println(list.get(1));
        System.out.println(list.get(2));

        // 便利输出
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

Output result:
hello world
java
[]

hello
world
java
hello
world
java


Summary: Add add, delete remove, check get, and get all sizes.


嘻嘻硕
27 声望12 粉丝

想当一只天然呆的鸭qwq