The list in Java is a collection object that centralizes all of the objects together. You can put any Java object in the list, or you can place the value directly.
The use method is simple, similar to an array.
You must introduce java.util.* in the program header before using the list
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) to list A;
A.add (2);
System.out.println (a);
A.remove (0);//Remove the No. 0 element in List A, that is, 1
System.out.println (a);
}
The results of the program's operation are as follows:
[1]
[1, 2]
[2]
List is often used for storing and manipulating a group of objects, such as a group of student information, a set of account information, and so on.
List is a collection interface, as long as the collection class interface will have an "iterator" (iterator), using this iteration, you can operate on a set of objects in the list memory.
All you need to do 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 define the number of arrays to be used, and the container object does not need to define the total number of array subscript.
Add a new member object with the Add () method, he can add only objects, cannot add basic data types, container also corresponds to get (), remove () method to obtain and delete data members
Instance 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 encapsulated 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 next if there is a continuation.
while (It.hasnext ()) {
//Set It.next encapsulation class, invoke integer Intvalue method return value to int to I;
int i= ((Integer) It.next ()). Intvalue ();
System.out.println ("Element in list is: " +i);}}
Instance 2.
Import java.util.*;
public class arraylisttest1{public
static void Main (String dd[]) {
//new a storage list
list l=new ArrayList (); c5/>//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 next if there is a continuation.
while (It.hasnext ()) {
//Set It.next encapsulation class, invoke cast string type assignment to I;
String i= (String) it.next ();
System.out.println ("Element in list is: " +i);}}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.