Table of contents:
1. Add, get, and delete elements in list;
2. Whether a certain element is included in the list;
3. Change element values according to the index in list (replace);
4. View (judgment) the index of the element in the list;
5. Judgment based on element index position;
6. Use the index position in the list to regenerate a new list (intercept the collection);
7. Compare all elements in the two lists;
8. Determine whether the list is empty;
9. Return the Iterator collection object;
10. Convert the collection to a string;
11. Convert the collection to an array;
12. Collection type conversion;
Note: The code in the content is related.
1. Add, get, and delete elements in list;
The addition method is: .add(e);
The method of obtaining is: .get(index);
The method of deletion is: .remove(index);
Delete according to the index; .remove(Object o);
Delete according to the content of the element;
List<String> person=new ArrayList<>(); person.add("jackie"); //Index is 0 //.add(e) person.add("peter"); //Index is 1 person.add("annie"); //Index is 2 person.add("martin"); //Index is 3 person.add("mary"); //Index is 4 person.remove(3); //.remove(index) person.remove("mary"); //.remove(Object o) String per=""; per=person.get(1); System.out.println(per); ////.get(index) for (int i = 0; i < person.size(); i++) { System.out.println(person.get(i)); //.get(index) }2. Whether a certain element is included in the list;
Method: .contains(Object o); Return true or false
List<String> fruits=new ArrayList<>(); fruits.add("Apple"); fruits.add("banana"); fruits.add("peach"); //for loop traversal list for (int i = 0; i < fruits.size(); i++) { System.out.println(fruits.get(i)); } String appleString="Apple"; //true or false System.out.println("fruits contains apples: "+fruits.contains(appleString)); if (fruits.contains(appleString)) { System.out.println("I like to eat apples"); }else { System.out.println("I'm not happy"); }3. Change element values according to the index in list (replace);
Note that the difference between .set(index, element); and .add(index, element);
String a="White Dragon Horse", b="Sha Monk", c="Bajie", d="Tang Monk", e="Wukong"; List<String> people=new ArrayList<>(); people.add(a); people.add(b); people.add(c); people.set(0, d); //.set(index, element); //.set(index, element); //.add(index, element); //.add(index, element); //.add(index, element); //.add(index, monk b of the original position moved one back//Enhanced for loop traversal list for(String str:people){ System.out.println(str); }4. View (judgment) the index of the element in the list;
Note: .indexOf(); is different from lastIndexOf();
List<String> names=new ArrayList<>(); names.add("Liu Bei"); //Index is 0 names.add("Guan Yu"); //Index is 1 names.add("Zhang Fei"); //Index is 2 names.add("Liu Bei"); //Index is 3 names.add("Zhang Fei"); //Index is 4 System.out.println(names.indexOf("Liu Bei")); System.out.println(names.lastIndexOf("Liu Bei")); System.out.println(names.indexOf("Zhang Fei")); System.out.println(names.lastIndexOf("Zhang Fei"));5. Judgment based on element index position;
if (names.indexOf("Liu Bei")==0) { System.out.println("Liu Bei is here"); }else if (names.lastIndexOf("Liu Bei")==3) { System.out.println("Liu Bei is there"); }else { System.out.println("Where is Liu Bei?"); }6. Use the index position in the list to regenerate a new list (intercept the collection);
Method: .subList(fromIndex, toIndex); .size(); This method obtains the sum of the elements in the list
List<String> phone=new ArrayList<>(); phone.add("Samsung"); //Index is 0 phone.add("Apple"); //Index is 1 phone.add("Hammer"); //Index is 2 phone.add("Huawei"); //Index is 3 phone.add("Xiaomi"); //Index is 4 //Original list for(String pho:phone){ System.out.println(pho); } //Generate new list phone=phone.subList(1, 4); //.subList(fromIndex, toIndex) //Use the object with index 1-4 to regenerate a list, but does not contain elements with index 4, 4-1=3 for (int i = 0; i < phone.size(); i++) { // phone.size() This method obtains the sum of the number of elements in the list System.out.println("The element contained in the new list is "+phone.get(i)); }7. Compare all elements in the two lists;
//The equals method of two equal objects must be true, but two hashcode equal objects may not be equal objects
//1.<BR>if (person.equals(fruits)) { System.out.println("All elements in both lists are the same"); }else { System.out.println("All elements in both lists are different"); } //2. if (person.hashCode()==fruits.hashCode()) { System.out.println("We are the same"); }else { System.out.println("We are the same"); }8. Determine whether the list is empty;
//Return true if empty, and return false if non-empty.
if (person.isEmpty()) { System.out.println("empty"); }else { System.out.println("not empty"); }9. Return the Iterator collection object;
System.out.println("Returns Iterator collection object:"+person.iterator());1+0. Convert the set to a string;
String liString=""; liString=person.toString(); System.out.println("Convert collection to string:"+liString);11. Convert the collection to an array;
System.out.println("Convert collection to array:"+person.toArray());12. Collection type conversion;
//1.Default type List<Object> listsStrings=new ArrayList<>(); for (int i = 0; i < person.size(); i++) { listsStrings.add(person.get(i)); } //2.Specify type List<StringBuffer> lst=new ArrayList<>(); for(String string:person){ lst.add(StringBuffer(string)); }Attached with full code:
package MyTest01; import java.util.ArrayList; import java.util.List; public class ListTest01 { public static void main(String[] args) { // Add, get, delete elements from list List<String> person=new ArrayList<>(); person.add("jackie"); // Index is 0 //.add(e) person.add("peter"); // Index is 1 person.add("annie"); // Index is 2 person.add("martin"); // Index is 3 person.add("mary"); // Index is 4 person.remove(3); //.remove(index) person.remove("mary"); //.remove(Object o) String per=""; per=person.get(1); System.out.println(per); ///.get(index) for (int i = 0; i < person.size(); i++) { System.out.println(person.get(i)); //.get(index) } // Whether list always contains a certain element List<String> fruits=new ArrayList<>(); fruits.add("Apple"); fruits.add("banana"); fruits.add("peach"); //for loop traversal list for (int i = 0; i < fruits.size(); i++) { System.out.println(fruits.get(i)); } String appleString="Apple"; //true or false System.out.println("fruits contains apples: "+fruits.contains(appleString)); if (fruits.contains(appleString)) { System.out.println("I like to eat apples"); }else { System.out.println("I am not happy"); } //Change element values according to the index in the list (replace) String a="White Dragon Horse", b="Sha Monk", c="Bajie", d="Tang Monk", e="Wukong"; List<String> people=new ArrayList<>(); people.add(a); people.add(b); people.add(c); people.set(0, d); //.set(index, element) //Put d Tang Monk to the position indexed to 0 in the list, replace a white dragon horse people.add(1, e); //.add(index, element); //Put eWukong to the position indexed to 1 in the list, and move one by one in the original position b Sand Monk back//Enhance the for loop traversal list for(String str:people){ System.out.println(str); } //Index of view (judgment) elements in list List<String> names=new ArrayList<>(); names.add("Liu Bei"); //Index is 0 names.add("Guan Yu"); //Index is 1 names.add("Zhang Fei"); //Index is 2 names.add("Liu Bei"); //Index is 3 names.add("Zhang Fei"); //Index is 4 System.out.println(names.indexOf("Liu Bei")); System.out.println(names.lastIndexOf("Liu Bei")); System.out.println(names.indexOf("Zhang Fei")); System.out.println(names.lastIndexOf("Zhang Fei")); //Judgement based on element index position if (names.indexOf("Liu Bei")==0) { System.out.println("Liu Bei here"); }else if (names.lastIndexOf("Liu Bei")==3) { System.out.println("Liu Bei is there"); }else { System.out.println("Where is Liu Bei? "); } //Use the index position in the list to regenerate a new list (intercept the collection) List<String> phone=new ArrayList<>(); phone.add("Samsung"); //Index is 0 phone.add("Apple"); //Index is 1 phone.add("Hammer"); //Index is 2 phone.add("Huawei"); //Index is 3 phone.add("Xiaomi"); //Index is 4 //Original list for(String pho:phone){ System.out.println(pho); } //Generate new list phone=phone.subList(1, 4); //.subList(fromIndex, toIndex) //Regenerate a list using the object with index 1-4, but does not contain the element with index 4, 4-1=3 for (int i = 0; i < phone.size(); i++) { // phone.size() This method obtains the sum of the number of elements in the list System.out.println("The element contained in the new list is "+phone.get(i)); } //Compare all elements in the two lists//The equals method of two equal objects must be true, but the two hashcode equal objects may not be equal objects if (person.equals(fruits)) { System.out.println("all elements in the two lists are the same"); }else { System.out.println("All elements in the two lists are different"); } if (person.hashCode()==fruits.hashCode()) { System.out.println("We are the same"); }else { System.out.println("We are different"); } //Judge whether the list is empty//Return true if it is empty, and return false if (person.isEmpty()) { System.out.println("Empty"); }else { System.out.println("Not empty"); } //Return the Iterator collection object System.out.println("Return the Iterator collection object:"+person.iterator()); //Convert the collection to a string String liString=""; liString=person.toString(); System.out.println("Convert the collection to a string:"+liString); //Convert the collection to an array, default type System.out.println("Convert the collection to an array:"+person.toArray()); ///Convert the collection to a specified type (friendly processing) //1. Default type List<Object> listsStrings=new ArrayList<>(); for (int i = 0; i < person.size(); i++) { listsStrings.add(person.get(i)); } //2. Specify type List<StringBuffer> lst=new ArrayList<>(); for(String string:person){ lst.add(StringBuffer(string)); } } private static StringBuffer StringBuffer(String string) { return null; } }The above detailed explanation of the commonly used operations of List collections in Java is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.