List collection creation and method

1. Create List

List <String> <链表名>=new ArrayList<>();

List <String> <链表名>=new LinkedList<>();

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Fire {
    
    public static void main(String[] args) {
        List <String> strsArray=new ArrayList<>();
        List <String> strsLinked=new LinkedList<>();    
    }
}

2. Add elements to the end of the list

strs.add("Aaa");

3. Clear the list

strs.clear();

4. Add elements at the specified location

strs.add(2, "Xxx");

5. Get the length of the list

int a=strs.size();

6. Determine whether the list is empty

boolean flag=strs.isEmpty();

7. Delete elements

strs.remove(2);//Delete by position

 strs.remove("Ddd");//remove by element

8. Get a location element

String str=strs.get(2);

9. Does the list contain an element?

boolean flag=strs.contains("Qqq");

10. Get the position of an element

int a=strs.indexOf("Aaa");//The position of the first occurrence
int b=strs.lastIndexOf("Aaa");//The position of the last occurrence

11. Intercept the collection

strs=strs.subList(2, 4);//The second and third element of the original set

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/SignalFire/article/details/105464520