List of Java Basics

1. List Introduction

List is a relatively important interface in the collection system. It has the characteristics of single-column storage, that is, only one element can be added at a time. It has three characteristics:
可重复:当存入相同数据时,可以有重复的数据存入,这是因为List集合有带索引的方法,可以通过索引值不同来辨别那些相同的数据。
存取顺序一致:存入数据的顺序与输出数据的顺序一定一致。
有带索引的方法:可以直接通过索引得到相应数据,可以直接使用普通for循环来遍历集合。

Next, we will combine the code to learn List related knowledge in detail.

2. Common methods of ListList应知应会

List is used as an interface, and its most commonly used implementation classes are ArrayList and LinkedList. As an interface, it extracts the common methods of all implementation classes, and all implementation classes that implement the List interface can use these methods. For example, both ArrayList and LinkedList can use the following common methods.

method name illustrate
void add(int index,E element) inserts the specified element at the specified position in this collection
E remove(int index) Delete the element at the specified index and return the deleted element
E set(int index,E element) Modify the element at the specified index and return the modified element
E get(int index) Returns the element at the specified index

2.1 Call the add() method to add data (you can specify the location to add)

The specific implementation class is List of ArrayList

public static void main(String[] args) {
    
    
        //创建一个数据类型为Integer的List对象,并指明其实现类为ArrayList
        List<Integer> list=new ArrayList<>();
        //调用add()方法增添数据
        list.add(15);
        list.add(17);
        list.add(19);
        list.add(20);
        //调用add()方法向指定位置插入元素
        list.add(1,16);
        list.add(3,18);
        //普通输出
        System.out.println(list);
    }

operation result:
insert image description here


The specific implementation class is List of LinkedList

public static void main(String[] args) {
    
    
        //创建一个数据类型为Integer的List对象,并指明其实现类为LinkedList
        List<Integer> list=new LinkedList<>();
        //调用add()方法增添数据
        list.add(15);
        list.add(17);
        list.add(19);
        list.add(20);
        //调用add()方法向指定位置插入元素
        list.add(1,16);
        list.add(3,18);
        //普通输出
        System.out.println(list);
    }

operation result:
insert image description here


2.2 Call the remove() method to delete the element at the specified position and return the deleted element

Next, let's take a List whose implementation class is LinkedList as a specific example.

public static void main(String[] args) {
    
    
        //创建一个数据类型为Integer的List对象,并指明其实现类为LinkedList
        List<Integer> list=new LinkedList<>();
        //调用add()方法增添数据
        list.add(15);
        list.add(16);
        list.add(17);
        list.add(18);
        list.add(18);
        list.add(19);
        list.add(20);
        //调用remove()方法删除指定位置元素并返回被删除元素
        Integer remove = list.remove(3);
        System.out.println("被删除的元素为:"+remove);
        //普通输出
        System.out.println(list);
    }

operation result:(From the running results, we can see that after calling the remove() method to delete 18, the position of the following elements will move to the left. This is for us to pay attention to the position of the index when deleting data in the future to prevent missing deletions.
insert image description here


2.3 Call the set() method to modify the element at the specified position and return the initial data

public static void main(String[] args) {
    
    
        //创建一个数据类型为Integer的List对象,并指明其实现类为LinkedList
        List<Integer> list=new LinkedList<>();
        //调用add()方法增添数据
        list.add(15);
        list.add(17);
        list.add(19);
        list.add(20);
        //调用add()方法向指定位置插入元素
        list.add(1,16);
        list.add(3,18);
        //调用set()方法修改指定位置元素并返回初始数据
        Integer set = list.set(3, 188);
        System.out.println(set);
    }

operation result:
insert image description here


2.4 Call the get() method to return the specified position element

public static void main(String[] args) {
    
    
        //创建一个数据类型为Integer的List对象,并指明其实现类为LinkedList
        List<Integer> list=new LinkedList<>();
        //调用add()方法增添数据
        list.add(15);
        list.add(17);
        list.add(19);
        list.add(20);
        //调用add()方法向指定位置插入元素
        list.add(1,16);
        list.add(3,18);
        //调用get()方法返回指定位置元素
        Integer integer = list.get(1);
        System.out.println(integer);
    }

operation result:
insert image description here


3. List can be repeated

We add two aoao and two daidai data

public static void main(String[] args) {
    
    
        //创建一个数据类型为String的List对象,并指明其实现类为LinkedList
        List<String> list=new LinkedList<>();
        //调用add()方法增添数据
        list.add("aoao");
        list.add("aoao");
        list.add("daidai");
        list.add("daidai");
        //调用add()方法向指定位置插入元素
        list.add(1,"zhangsan");
        list.add(3,"lisi");
        //普通输出
        System.out.println(list);
    }

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


4. The order of List access is the same

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

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

operation result:
insert image description here


5. List has an indexed method

Because List has an indexed method, the corresponding data can be obtained directly through the index. You can use ordinary for loops, ordinary output statements, forEach statements and Iterator iterators to traverse the collection.

5.1 Ordinary output statement output List collection

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

operation result:
insert image description here


5.2 Ordinary for loop output List collection

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

operation result:
insert image description here


5.3 The forEach statement outputs the List collection

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

operation result:
insert image description here


5.4 Iterator iterator output List collection

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

operation result:
insert image description here


6. List deletes the data of the specified value

6.1 Delete the data of the specified value and delete the data常见问题

public static void main(String[] args) {
    
    
        //创建一个数据类型为Integer的List对象,并指明其实现类为LinkedList
        List<Integer> list=new LinkedList<>();
        //调用add()方法增添数据
        list.add(15);
        list.add(18);
        list.add(18);
        list.add(16);
        list.add(17);
        list.add(18);
        list.add(19);
        list.add(20);
        list.add(18);
        //List删除值为18的元素
        for (int i = 0; i < list.size(); i++) {
    
    
            if(list.get(i).equals(18)){
    
    
                list.remove(i);
            }
        }
        //普通输出
        System.out.println(list);
    }

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

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

First we insert the collection data elements in the following order:
Please add a picture description


When we delete the first 18, the collection becomes as shown below: (It is not difficult for us to conclude that when we delete a certain data in the List collection, the collection will automatically shrink. The elements after the deleted element will be shifted to the left, that is, after we delete the element whose index is 2 and the value is 18 after we delete 18, it will become the index is 1 and the value is 18 after moving to the left
Please add a picture description


But at this time, our i index points to 2, so a 18 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) {
    
    
        //创建一个数据类型为Integer的List对象,并指明其实现类为LinkedList
        List<Integer> list=new LinkedList<>();
        //调用add()方法增添数据
        list.add(15);
        list.add(18);
        list.add(18);
        list.add(16);
        list.add(17);
        list.add(18);
        list.add(19);
        list.add(20);
        list.add(18);
        //List删除值为18的元素
        for (int i = 0; i < list.size(); i++) {
    
    
            if(list.get(i).equals(18)){
    
    
                list.remove(i);
                //之所以i要减一,是因为当我们删除第一个18后,之前第一个18之后的所有数据会左移,那么第二个18就会左移至第一个18的位置,如果我们的i不减1,那么就会跳过第二个18而不删除它
                i=i-1;
            }
        }
        //普通输出
        System.out.println(list);
    }

operation result:
insert image description here


OK! ! ! List introduction is over! ! !

Guess you like

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