Lists are often used to store and manipulate a group of objects, such as a group of student information, a group of account information, and so on.
List is a collection interface, as long as the collection class interface will have an "iteration" (Iterator), using this iteration, you can work on a set of objects of list memory.
All that is needed to manipulate this list memory is to get an example of this iteration: Iterator it=l.iterator ();
Can be understood as a dynamic array, the traditional array must be defined so that the number of arrays can be used, and the container object does not need to define the array subscript total.
Add a new member object with the Add () method, he can add only to the object, cannot add the base data type, the container also corresponds to the Get (), remove () method to get and delete data members
Example 1.
Import java.util.*;
public class arraylisttest{
public static void Main (String dd[]) {
New has a storage list
List l=new ArrayList ();
Because the collection framework can store only objects, the new wrapper 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 of the current value. His operation is to determine if there is a value for the next if there is a continuation.
while (It.hasnext ()) {
Set the It.next encapsulation class, call the integer Intvalue method return value to int assigned 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 has a storage list
List l=new ArrayList ();
Because the collection framework can only store objects This example is to show that string is an object
L.add ("Lalala");
L.add ("AFDSFA");
Iterator It=l.iterator ();
Hasnext is the value of the current value. His operation is to determine if there is a value for the next if there is a continuation.
while (It.hasnext ()) {
Set It.next encapsulation class, call cast string type assignment to I;
String i= (String) it.next ();
System.out.println ("Element in list is:" +i);
}
}
}
A popular introduction and simple example of list in Java