A popular introduction and simple example of list in Java

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.