Notes when using the List interface in java

Source: Internet
Author: User

The List interface inherits from the Collection interface... the element sequence is inserted in the order of... there are 2 lists.

ArrayList and sorted list are variable-length arrays, but they are stored in different ways .. ArrayList is stored in arrays and is suitable for searching elements ,,

The sort list is a linked list storage structure suitable for efficient insertion and deletion.

Both the listlist and ArrayList implement the List interface.

The Iterable interface can be used to iterate for loop operations... Elements cannot be deleted through the set of iterations for loop...

When using list, we often need to return a sublist of List.

We know that there is a subList method in the List interface that can return the index length from 0 to List

A subview of the element. This view maps to the original list. adding or deleting a subview will affect the original list;

Looking at the following code, we found that we first deleted all elements in the view to delete all the Child list elements in the list. The result is null.

We have added elements to the original list through the sublist view. We can see that the elements are added to the right of the original list...

Import java. util. ArrayList;
Import java. util. List;
Class MyObject {
Static int I = 0;
@ Override
Public String toString (){
// TODO Auto-generated method stub
Return this. getClass (). getName ();
}
}
Public class MyArray {
Private List <MyObject> l = new ArrayList <MyObject> ();
Public void add (MyObject... obj ){
For (MyObject tem: obj ){
L. add (tem );
}
}
Public void show (){
System. out. println (this. l );
}
Public static void main (String [] args ){
MyArray arr = new MyArray ();
MyObject ss [] = {new MyObject (), new MyObject ()};
Arr. add (ss );
System. out. println ("Raw data ");
Arr. show ();
List <MyObject> ll = arr. getSub (); // returns a List Of all values between 0 and 4 excluding index 4.
System. out. println ("deleting all elements from the subview ");
Ll. removeAll (ll); // delete all lists from the sublist
Arr. show ();
System. out. println ("add through subview ");
Ll. add (MyObject) new MyObject ());
Ll. add (MyObject) new MyObject ());
Ll. add (MyObject) new MyObject ());
Arr. show ();
}
Public List <MyObject> getSub (int I, int j ){
Return l. subList (I, j );
}

}

Author: yue7603835

 

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.