Java list ArrayList usage details

Source: Internet
Author: User

List is an interface, while ListArray is a class.
ListArray inherits and implements the List.
Therefore, the List cannot be constructed, but a reference can be created for the List as above, and ListArray can be constructed.
List list; // correct list = null;
List list = new List (); // Incorrect usage

List list = new ArrayList (); this statement creates an ArrayList object and traces it to List. In this case, it is a List object, and some ArrayList has attributes and methods that are not available in List, so it cannot be reused.
While ArrayList list = new ArrayList (); when an object is created, all attributes of ArrayList are retained.

This is an example:
Import java. util .*;

Public class TestList {
Public static void main (String [] args ){
List list = new ArrayList ();
ArrayList arrayList = new ArrayList ();

List. trimToSize (); // error. This method is not available.
ArrayList. trimToSize (); // This method is available in ArrayList.
}
}

List usage
List includes all the implementation classes of the List interface and List interface. Because the List interface implements the Collection interface, the List interface has all the common methods provided by the Collection interface. Because List is a List type, the List interface also provides some common methods suitable for itself, see table 1.

Table 1 common methods and functions defined by the List interface
From Table 1, we can see that the common methods provided by the List interface are related to indexes. This is because the List set is of the List type and objects are stored linearly, you can use the index of an object to operate an object.
Common implementation classes of the List interface include ArrayList and listlist. When using the List set, it is usually declared as the List type. During instantiation, it is instantiated as the ArrayList or listlist according to the actual needs, for example:
List <String> l = new ArrayList <String> (); // use the ArrayList class to instantiate a List set.
List <String> l2 = new consumer List <String> (); // use the consumer List class to instantiate a List set.
1. Differences between the add (int index, Object obj) method and the set (int index, Object obj) method
When using the List set, note that the add (int index, Object obj) method and set (int index, Object obj) method are distinguished. The former is to add an Object to the specified index location, the latter is to modify the object at the specified index location. For example, execute the following code:
Key code of srccommwqTestCollection. java:

Public static void main (String [] args ){
String a = "A", B = "B", c = "C", d = "D", e = "E ";
List <String> list = new partition List <String> ();
List. add ();
List. add (e );
List. add (d );
List. set (1, B); // modify object e with index position 1 to object B.
List. add (2, c); // add object c to the location where the index is 2
Iterator <String> it = list. iterator ();
While (it. hasNext ()){
System. out. println (it. next ());
}
}
The following information is output in the console:
A
B
C
D

Because the List set can access objects through the index location, you can also traverse the List set through the for loop. for example, the code for traversing the List set in the above code is as follows:

Key code of srccommwqTestCollection. java:
For (int I = 0; I <list. size (); I ++ ){
System. out. println (list. get (I); // use the get (int index) method to obtain the object at the specified index location
}
The complete code of srccommwqTestCollection. java is as follows:
Package com. mwq;
Import java. util. ArrayList;
Import java. util. Collections list;
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 partition List <String> ();
List. add ();
List. add (e );
List. add (d );
List. set (1, B); // modify object e with index position 1 to object B.
List. add (2, c); // add object c to the location where the index is 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 obtain the object at the specified index location
//}
System. out. println ("end! ");
}
}

 

Compile it and you will know the result.

If it looks like this:
List a = new ArrayList ();
Then, a has all the attributes and methods of List and ArrayList, which will not be reduced.
If the List and ArrayList have the same attributes (such as int I), there are the same methods (such as void f ()),
A. I calls
A. f () is to call f () in ArrayList ();
---------------------------------------------------------------
Key issues:
Why use List list = new ArrayList () instead of ArrayList alist = new ArrayList?
The problem is that List has multiple implementation classes. Now you are using ArrayList. Maybe you need to change to another implementation class on that day, such as struct List or Vector, now you only need to change this line:
List list = new external list (); other code that uses the List does not need to be modified.
Suppose you start to use ArrayList alist = new ArrayList (), and you have modified it, especially if you use the methods and attributes unique to ArrayList.


The region is defined by List arr = new ArrayList (); the industry is defined by ArrayListarr = new ArrayList (); it indicates that the special ArrayList method is used in the industry.

Another example is to declare the following in the class method:
Private void doMyAction (List list ){}
In this way, this method can process all classes that implement the List interface and implement generic functions to a certain extent.

If you think that the performance of ArrayList and HashMap cannot meet your needs during development, you can customize your custom classes by implementing List, Map (or Collection).

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.