List in Java is a collection object that stores all objects together. You can put any Java object in List or you can put values directly.
The usage is simple, similar to an array.
Before using List, you must import java.util.* in the program header.
import java.util.*;public class list { public static void main(String args[]) { List a=new ArrayList(); a.add(1);//Add 1 System.out.println(a); a.add(2); System.out.println(a); a.remove(0);//Remove the 0th element in LIST a, that is, 1 System.out.println(a); }}The running results of the program are as follows:
[1]
[1, 2]
[2]
List is often used to store and operate a group of objects, such as a group of student information, a group of account information, etc.
List is a collection interface. As long as it is a collection class interface, it will have an "iterator". Using this iterator, you can operate on a set of objects in the list memory.
If you want to operate this list memory, you must first get an instance of this iterator: Iterator it=l.iterator();
It can be understood as a dynamic array. Traditional arrays must define the number of arrays before they can be used, and container objects do not need to define the total number of array subscripts.
Use the add() method to add a new member object. All it can add is an object, not a basic data type. The container also corresponds to the get() and remove() methods to get and delete data members.
Example 1.
import java.util.*;public class ArrayListTest{public static void main(String dd[]){ //new a storage list List l=new ArrayList(); //Because the Collection framework can only store objects, new encapsulation class l.add(new Integer(1)); l.add(new Integer(2)); l.add(new Integer(3)); l.add(new Integer(4)); Iterator it=l.iterator(); //hasNext is the value that takes the current value. Its calculation process is to determine whether the next value has a value if it continues. while(it.hasNext()){ //Suppose it.next encapsulation class, call Integer's intValue method to get the return value as int to i; int i=((Integer)it.next()).intValue(); System.out.println("Element in list is : "+i); }}} Example 2.
import java.util.*;public class ArrayListTest1{public static void main(String dd[]){ //new a store list List l=new ArrayList(); //Because the Collection framework can only store objects. This example is to illustrate that String is an object l.add("lalala"); l.add("afdsfa"); Iterator it=l.iterator(); //hasNext is the value that takes the current value. Its calculation process is to determine whether the next value has a value and if it continues. while(it.hasNext()){ //Suppose it.next encapsulate the class and call the cast String type assigns the value to i; String i=(String)it.next(); System.out.println("Element in list is : "+i); }}}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.