ArrayList based on Java

1. Introduction to ArrayList

ArrayList is an implementation class of the List interface (ArrayList implements the List interface), which has the characteristics of List. The underlying structure of ArrayList is an array.

Please add a picture description

ArrayList has no other characteristics, and the characteristics of List are its characteristics:
可重复:当存入相同数据时,可以有重复的数据存入,这是因为List集合有带索引的方法,可以通过索引值不同来辨别那些相同的数据。
存取顺序一致:存入数据的顺序与输出数据的顺序一定一致。
有带索引的方法:可以直接通过索引得到相应数据,可以直接使用普通for循环来遍历集合。

Next, we will combine the code to learn about ArrayList in detail.

Two, ArrayList unique methodArrayList应知应会

Since ArrayList is the implementation class of List interface and Collection interface, the methods owned by List and Collection can be used by ArrayList, and will not be described in detail here.

method name illustrate
boolean addAll(Collection c) Add a Collection collection to the ArrayList
boolean removeAll(Collection c) Delete all data elements in the ArrayList in the specified Collection
List subList(int fromIndex,int toIndex) Intercept some elements of ArrayList

2.1 Call addAll() method to add a Collection collection to ArrayList

Let's review the interfaces and common implementation classes contained in Collection

Please add a picture description


Call the addAll() method to add an ArrayList collection to the ArrayList

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();

        //再次创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList1=new ArrayList<>();
        //调用add()方法增添数据
        arrayList1.add("a");arrayList1.add("b");arrayList1.add("c");

        //调用addAll()方法将一个ArrayList集合添加到ArrayList中
        arrayList.addAll(arrayList1);

        //普通输出
        System.out.println(arrayList);
    }

operation result:

insert image description here


Call the addAll() method to add a LinkedList collection to the ArrayList

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();

        //创建一个数据类型为String的LinkedList对象
        LinkedList<String> list=new LinkedList<>();
        //调用add()方法增添数据
        list.add("aa");list.add("bb");list.add("cc");

        //调用addAll()方法将一个LinkedList集合添加到ArrayList中
        arrayList.addAll(list);

        //普通输出
        System.out.println(arrayList);
    }

operation result:
insert image description here


Call the addAll() method to add a HashSet collection to the ArrayList

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();

        //创建一个数据类型为String的HashSet对象
        HashSet<String> hashSet=new HashSet<>();
        //调用add()方法增添数据
        hashSet.add("aaa");hashSet.add("bbb");hashSet.add("ccc");hashSet.add("ccc");

        //调用addAll() 方法将一个HashSet集合添加到ArrayList中
        arrayList.addAll(hashSet);

        //普通输出
        System.out.println(arrayList);
    }

operation result:(We see that when the collection we store is a Set collection, the access order will become inconsistent, and there will be no duplicate data
insert image description here


Call the addAll() method to add a TreeSet collection to the ArrayList

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();

        //创建一个数据类型为String的TreeSet对象
        TreeSet<String> treeSet=new TreeSet<>();
        //调用add()方法增添数据
        treeSet.add("aaaa");treeSet.add("cccc");treeSet.add("bbbb");treeSet.add("cccc");

        //调用addAll() 方法将一个TreeSet集合添加到ArrayList中
        arrayList.addAll(treeSet);

        //普通输出
        System.out.println(arrayList);
    }

operation result:(We see that when the collection we store is a Set collection, the access order will become inconsistent, and there will be no duplicate data
insert image description here


2.2 Call the removeAll() method to delete all data elements in the ArrayList in the specified Collection

Next, we will no longer list them one by one like addAll(), because these operations are the same.

Call the removeAll() method to delete a TreeSet collection from the ArrayList

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();
        //调用add()方法增添数据
        arrayList.add("a");arrayList.add("b");arrayList.add("c");

        //创建一个数据类型为String的TreeSet对象
        TreeSet<String> treeSet=new TreeSet<>();
        //调用add()方法增添数据
        treeSet.add("aaaa");treeSet.add("cccc");treeSet.add("bbbb");treeSet.add("cccc");

        //调用addAll()方法将一个TreeSet集合添加到ArrayList中
        arrayList.addAll(treeSet);

        //调用removeAll() 方法将一个TreeSet集合从ArrayList中删除
        arrayList.removeAll(treeSet);

        //普通输出
        System.out.println(arrayList);
    }

operation result:
insert image description here


2.3 Call the subList() method to intercept some elements of ArrayList

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();
        //调用add()方法增添数据
        arrayList.add("a");arrayList.add("b");arrayList.add("c");

        //创建一个数据类型为String的TreeSet对象
        TreeSet<String> treeSet=new TreeSet<>();
        //调用add()方法增添数据
        treeSet.add("aaaa");treeSet.add("cccc");treeSet.add("bbbb");treeSet.add("cccc");

        //调用addAll()方法将一个TreeSet集合添加到ArrayList中
        arrayList.addAll(treeSet);

        //调用subList()方法截取部分ArrayList的元素
        List<String> strings = arrayList.subList(2, 4);
        //普通输出截取元素
        System.out.println(strings);
    }

operation result:
insert image description here


Three, ArrayList repeatable features

We add two c and two c data

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();
        //调用add()方法增添数据
        arrayList.add("a");
        arrayList.add("b");
        arrayList.add("c");
        arrayList.add("c");
        //普通输出
        System.out.println(arrayList);
    }

operation result:(We see that duplicate data will also be output
insert image description here


Fourth, the ArrayList access sequence is consistent

The order in which we store data is aoao, daidai, bobo, lisi, wangwu, zhangsan

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();
        //调用add()方法增添数据
        arrayList.add("aoao");
        arrayList.add("daidai");
        arrayList.add("bobo");
        arrayList.add("lisi");
        arrayList.add("wangwu");
        arrayList.add("zhangsan");
        //普通输出
        System.out.println(arrayList);
    }

operation result:
insert image description here


Five, ArrayList has an index method

Because ArrayList has a method with an index, the corresponding data can be obtained directly through the index, and the collection can be traversed by using ordinary for loops, ordinary output statements, forEach statements and Iterator iterators.

5.1 Common output statement output ArrayList collection

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();
        //调用add()方法增添数据
        arrayList.add("aoao");
        arrayList.add("daidai");
        arrayList.add("bobo");
        arrayList.add("lisi");
        arrayList.add("wangwu");
        arrayList.add("zhangsan");
        //普通输出
        System.out.println(arrayList);
    }

operation result:
insert image description here


5.2 Ordinary for loop output ArrayList collection

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();
        //调用add()方法增添数据
        arrayList.add("aoao");
        arrayList.add("daidai");
        arrayList.add("bobo");
        arrayList.add("lisi");
        arrayList.add("wangwu");
        arrayList.add("zhangsan");
        //普通for循环输出ArrayList集合
        for (int i = 0; i < arrayList.size(); i++) {
    
    
            System.out.println(arrayList.get(i));
        }
    }

operation result:
insert image description here


5.3 The forEach statement outputs the ArrayList collection

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();
        //调用add()方法增添数据
        arrayList.add("aoao");
        arrayList.add("daidai");
        arrayList.add("bobo");
        arrayList.add("lisi");
        arrayList.add("wangwu");
        arrayList.add("zhangsan");
        //forEach语句输出ArrayList集合
        //s即为list遍历的结果,String即为Arraylist的数据类型
        for (String s : arrayList) {
    
    
            System.out.println(s);
        }
    }

operation result:
insert image description here


5.4 Iterator iterator outputs ArrayList collection

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();
        //调用add()方法增添数据
        arrayList.add("aoao");
        arrayList.add("daidai");
        arrayList.add("bobo");
        arrayList.add("lisi");
        arrayList.add("wangwu");
        arrayList.add("zhangsan");
        //Iterator迭代器输出ArrayList集合
        Iterator<String> iterator=arrayList.iterator();
        //iterator.hasNext()的作用即为判断当前位置是否存在元素,若存在则返回true,否则返回false
        while(iterator.hasNext()){
    
    
            //iterator.next()的作用即为获取当前位置元素,并指向下一位置以便hashNext判断
            System.out.println(iterator.next());
        }
    }

operation result:
insert image description here


6. ArrayList deletes the data of the specified value

6.1 Delete the data of the specified value (missing deleted data problem)常见问题

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();
        //调用add()方法增添数据
        arrayList.add("daidai");
        arrayList.add("daidai");
        arrayList.add("aoao");
        arrayList.add("bobo");
        arrayList.add("lisi");
        arrayList.add("wangwu");
        arrayList.add("zhangsan");
        arrayList.add("daidai");
        //ArrayList删除值为daidai的元素
        for (int i = 0; i < arrayList.size(); i++) {
    
    
            if(arrayList.get(i).equals("daidai")){
    
    
                arrayList.remove(i);
            }
        }
        //普通输出
        System.out.println(arrayList);
    }

operation result:(We found that there is still a daidai that has not been deleted
insert image description here

原因:当我们删除某一元素后,之前此元素之后的所有数据会左移,那么如果第二个与此元素相等的元素,于此元素并排,它就会左移至此元素的位置,而我们的指针之后会加1,因此会跳过它。文字难以理解?没关系,上图解!!!

First, the order in which we insert the elements of the data collection is as follows:
Please add a picture description


When we delete the first daidai, the collection becomes as shown below: (It is not difficult for us to conclude that when we delete a certain data in the ArrayList collection, the collection will automatically shrink. The element after the deleted element will be shifted to the left, that is, after we delete the daidai, the element whose index was 1 and the value was daidai before shifting to the left, becomes the index 0 and the value is daidai
Please add a picture description


But at this time, our i index points to 1, so a daidai will be skipped in the subsequent traversal, and missing deletion occurs.
Please add a picture description


6.2 Solutions to missing and deleted data

When we delete a piece of data, in order to prevent missing deletion, the index i should also be moved to the left, i.e. i-1

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的ArrayList对象
        ArrayList<String> arrayList=new ArrayList<>();
        //调用add()方法增添数据
        arrayList.add("daidai");
        arrayList.add("daidai");
        arrayList.add("aoao");
        arrayList.add("bobo");
        arrayList.add("lisi");
        arrayList.add("wangwu");
        arrayList.add("zhangsan");
        arrayList.add("daidai");
        //ArrayList删除值为daidai的元素
        for (int i = 0; i < arrayList.size(); i++) {
    
    
            if(arrayList.get(i).equals("daidai")){
    
    
                arrayList.remove(i);
                //之所以i要减一,是因为当我们删除第一个daidai后,之前第一个daidai之后的所有数据会左移,那么第二个daidai就会左移至第一个daidai的位置,如果我们的i不减1,那么就会跳过第二个daidai而不删除它
                i=i-1;
            }
        }
        //普通输出
        System.out.println(arrayList);
    }

operation result:
insert image description here


OK! ! ! The introduction to ArrayList is over! ! !

Guess you like

Origin blog.csdn.net/qq_45344586/article/details/130032962