Java:list usage

Source: Internet
Author: User

1. You can add any object in the list, including the new class that you defined.
    • Class person{
      .....
      }
      The above defines a person class, and the following is a good way to use list
      Person P1=new person ();
      Person P2=new person ();
      List list=new ArrayList ();
      List.add (p1);
      List.add (p2);//This is the addition of objects to the list
      for (int i=0;i
      Person p= (person) list.get (i);///Note that it is important to enforce type conversions, because the objects that are taken out of the list are of type object, and I hope you will be helpful.

      }
2, list is an interface, can not be instantiated, need to instantiate a ArrayList or LinkedList
List myList = new ArrayList ();
3. Use Mylist.add (any object); You can add it.
4, the value of the time Mylist.get (index), the value is an object, the use of the type conversion required.
5. The iterator iterator can be used to iterate over the elements in the list.
The objects in the list collection are emitted in a certain order, and the contents can be duplicated.
List interface Implementation class: ArrayList (Implement dynamic Array), Vector (implement dynamic Array), LinkedList (implement linked list), stack (Implementation stack)







A list interface
The 1.java.util.arraylist class implements a dynamic array that can grow, usually using more than the Add () method, and other methods
Please refer to the API Help documentation. Use this method to add objects to the collection, and these objects are arranged in a certain order
, its internal principle is an array implementation, so the processing of large amounts of data is not recommended.

public class Testarraylist {

public static void Main (string[] args) {
Declare list and instantiate as ArrayList
List al = new ArrayList ();
Add an element using the Add () method
Al.add ("a");
Al.add ("B");
Al.add ("C");
Al.add ("D");
Use the iterator iterator to iterate through the elements of the collection and print
for (Iterator i = Al.iterator (); I.hasnext ();) {
String str = (string) i.next ();
System.out.println (str);
}
}

}
The 3.java.util.vector (vector) class is similar to ArrayList and implements a dynamic array that can grow arbitrarily to hold more objects.


public class Testarraylist {

public static void Main (string[] args) {
Declares a list and instantiates it as a vector
List al = new Vector ();
Add an object using the Add () method
Al.add ("a");
Al.add ("B");
Al.add ("C");
Al.add ("D");
Use the iterator iterator to iterate through the objects of the collection and print
for (Iterator i = Al.iterator (); I.hasnext ();) {
String str = (string) i.next ();
System.out.println (str);
}
}

}
The 3.java.util.linkedlist class implements a linked list, which can be initialized to an empty or existing collection, typically using the following methods
Add (); Adds an object to the end of the list.
AddFirst () Adds an object at the beginning of the list.
AddLast () Adds an object at the end of the list.
GetFirst () Gets the object at the beginning of the linked list.
GetLast () Gets the object at the end of the list.
Note that this class provides a way to randomly access elements in the list, but the underlying still has to traverse to find random-access objects, so performance is still limited.

public static void Main (string[] args) {
Declare LinkedList and instantiate
LinkedList al = new LinkedList ();
Add an element using the Add () method
Al.add ("a");
Al.add ("B");
Al.add ("C");
Al.add ("D");
Use the iterator iterator to iterate through the elements of the collection and print
for (Iterator i = Al.iterator (); I.hasnext ();) {
String str = (string) i.next ();
System.out.println (str);
}
System.out.println ("_____");
Add x and Z to the chain header and tail respectively
Al.addfirst ("Z");
Al.addlast ("X");
Traverse to view the results after adding
for (Iterator i = Al.iterator (); I.hasnext ();) {
String str = (string) i.next ();
System.out.println (str);
}

}

The 4.java.util.stack class implements the stack data structure, which is the principle of advanced post-exit. Can only be empty when created.
Adding objects using the push () method
public static void Main (string[] args) {
Declaring a stack and instantiating it
Stack al = new stack ();
Adding elements using the push () method
Al.push ("a");
Al.push ("B");
Al.push ("C");
Al.push ("D");
Al.push ("F");
Use the iterator iterator to iterate through the elements of the collection and print
for (Iterator i = Al.iterator (); I.hasnext ();) {
String str = (string) i.next ();
System.out.println (str);
}
}
-----------
Example added by myself:
public class Test {

public static void Main (string[] args) {
TODO auto-generated Method Stub
ArrayList list = new ArrayList ();
List.add (0, "AA");
List.add (1, "BB");
List.add (2, "CC");
for (int i=0;i<10;i++)
{

List.add (integer.tostring (i));
}
for (int i=0;i<list.size (); i++)
{
System.out.println (List.get (i));
}
For (Iterator i=list.iterator (); I.hasnext ();)
First, define a list object's iterator, and then use the iterator to iterate over the iteration
{
string s = (string) i.next ();
System.out.println (s);
}
}
}

Java:list usage

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.