Java Basic Syntax List

Source: Internet
Author: User

List: The elements are ordered (how to take out, the order will not be chaotic), the elements can be repeated (the angle of 1 on the 3, the angle of 2 can also have a 3) because the set system has an index,

ArrayList: The underlying data structure uses the array structure (the array length is variable 50% extension) ( characterized by a fast query, but the deletion of the slow) thread is out of sync

LinkedList: The underlying data structure is a linked list structure (characterized by slow query, deletion faster)
Vector: The underlying is the array data structure thread synchronization (array length is variable 100% extension) (whether the query or additions and deletions are slow, replaced by ArrayList)

Usage of List
The list includes all the implementation classes of the list interface and the list interface. Because the list interface implements the collection interface, the list interface has all the common methods provided by the collection interface, and since list is a list type, the list interface also provides some common methods that are appropriate for itself, as shown in table 1.

Table 1 Common methods and functions of the list interface definition
As can be seen from table 1, the list interface provides a common method that is appropriate for itself, since the list collection is a list type that stores objects in a linear manner and can manipulate objects through the object's index.
The common implementation classes of the list interface are ArrayList and LinkedList, which, when using the list collection, are typically declared as a list type, instantiated as ArrayList or LinkedList as needed for the actual situation.For example:
list<string> L = new arraylist<string> ();//Instantiate the list collection with the ArrayList class
list<string> L2 = new linkedlist<string> ();//Instantiate list collection with LinkedList class
1. The difference between the Add (int index, Object obj) method and the set (int index, object obj) method
When using the list collection, it is important to distinguish between the Add (int index, Object obj) method and the set (int index, Object obj) method, which adds an object to the specified index location, which is the object that modifies the specified index location, such as executing the following code:
Src/com/mwq/testcollection.java Key Code:
public static void Main (string[] args) {
String a = "a", B = "B", C = "C", d = "D", E = "E";
list<string> list = new linkedlist<string> ();
List.add (a);
List.add (e);
List.add (d);
List.set (1, b);//Modify the object E with index position 1 to object B
List.add (2, c);//Add object C to the position at index position 2
Iterator<string> it = List.iterator ();
while (It.hasnext ()) {
System.out.println (It.next ());
}
}
The console will output the following information:
A
B
C
D
Because the list collection can access the object through an indexed location, you can also traverse the list collection through a For loop, such as the code that iterates through the list collection in the code above:
Src/com/mwq/testcollection.java Key Code:
for (int i = 0; i < list.size (); i++) {
System.out.println (List.get (i));//Use the Get (int index) method to get the object at the specified index position
}
Src/com/mwq/testcollection.java complete code is as follows:
Package com.mwq;
Import java.util.ArrayList;
Import java.util.LinkedList;
Import Java.util.Iterator;
Import java.util.List;
public class Testcollection {
public static void Main (string[] args) {
System.out.println ("Start:");
String a = "a", B = "B", C = "C", d = "D", E = "E";
list<string> list = new linkedlist<string> ();
List.add (a);
List.add (e);
List.add (d);
List.set (1, b);//Modify the object E with index position 1 to object B
List.add (2, c);//Add object C to the position at index position 2
Iterator<string> it = List.iterator ();
while (It.hasnext ()) {
System.out.println (It.next ());
}
for (int i = 0; i < list.size (); i++) {
System.out.println (List.get (i));//Use the Get (int index) method to get the object at the specified index position
//          }
SYSTEM.OUT.PRINTLN ("End! ");
}
}
2. The difference between the IndexOf (object obj) method and the LastIndexOf (object obj) method
When using the list collection, it is important to distinguish between the indexof (object obj) method and the LastIndexOf (object obj) method, which is the smallest index position to get the specified object, which is the largest index position for the specified object. The precondition is that the specified object has duplicate objects in the list collection, otherwise if there is only one specified object in the list collection, the index position obtained by the two methods is the same, such as executing the following code:
Src/com/mwq/testcollection.java Key Code:
public static void Main (string[] args) {
String a = "a", B = "B", C = "C", d = "D", repeat = "repeat";
list<string> list = new arraylist<string> ();
List.add (a); Index position is 0
List.add (repeat); Index position is 1
List.add (b); Index position is 2
List.add (repeat); Index position is 3

List.add (c); Index position is 4
List.add (repeat); Index position is 5
List.add (d); Index position is 6
System.out.println (List.indexof (repeat));
System.out.println (List.lastindexof (repeat));
System.out.println (List.indexof (b));
System.out.println (List.lastindexof (b));
}
Src/com/mwq/testcollection.java complete code is as follows:
Package com.mwq;
Import java.util.ArrayList;
Import java.util.List;
public class Testcollection {
public static void Main (string[] args) {
System.out.println ("Start:");
String a = "a", B = "B", C = "C", d = "D", repeat = "repeat";
list<string> list = new arraylist<string> ();
List.add (a); Index position is 0
List.add (repeat); Index position is 1
List.add (b); Index position is 2
List.add (repeat); Index position is 3
List.add (c); Index position is 4
List.add (repeat); Index position is 5
List.add (d); Index position is 6
System.out.println (List.indexof (repeat));
System.out.println (List.lastindexof (repeat));
System.out.println (List.indexof (b));
System.out.println (List.lastindexof (b));
SYSTEM.OUT.PRINTLN ("End! ");
}
}
The console will output the following information:
1
5
2
2
3. sublist (int fromIndex, int toindex) method
When using the sublist (int fromIndex, int toindex) method to intercept some objects in an existing list collection to generate a new list collection, it is important to note that the newly generated collection contains the object represented by the starting index position, but does not contain the object that the terminating index position represents. For example, execute the following code:
Src/com/mwq/testcollection.java Key Code:
public static void Main (string[] args) {
String a = "a", B = "B", C = "C", d = "D", E = "E";
list<string> list = new arraylist<string> ();
List.add (a); Index position is 0
List.add (b); Index position is 1
List.add (c); Index position is 2
List.add (d); Index position is 3
List.add (e); Index position is 4
List = List.sublist (1, 3);//Regenerate a list collection with objects from index locations 1 to 3
for (int i = 0; i < list.size (); i++) {
System.out.println (List.get (i));
}
}
Src/com/mwq/testcollection.java Complete code:
Package com.mwq;
Import java.util.ArrayList;
Import java.util.List;
public class Testcollection {
public static void Main (string[] args) {
System.out.println ("Start:");
String a = "a", B = "B", C = "C", d = "D", E = "E";
list<string> list = new arraylist<string> ();
List.add (a); Index position is 0
List.add (b); Index position is 1
List.add (c); Index position is 2
List.add (d); Index position is 3
List.add (e); Index position is 4
List = List.sublist (1, 3);//Regenerate a list collection with objects from index locations 1 to 3
for (int i = 0; i < list.size (); i++) {
System.out.println (List.get (i));
}
SYSTEM.OUT.PRINTLN ("End! ");
}
}
The console will output the following information:
B
C

Java Basic Syntax List

Related Article

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.