A brief introduction to the use of list in Java _java

Source: Internet
Author: User

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.

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.