Java------List Usage

Source: Internet
Author: User
Tags addall

Define the list and put the array in it:

list<integer> temp = new arraylist<integer> ();

list<string> temp = new arraylist<string> ();

Get the number of elements in the set: List.size ();

To add an element:
Default add: List.add (e);
Specify the subscript to add (after adding the element after subscript, move one bit backwards): List.add (index,e);

If we get the list also want to delete, then you can use the AddAll method

string[] str = {"1", "2", "3",};
list<string> Strarray = new arraylist<string> ();
Strarray.addall (Arrays.aslist (str));
Strarray.add ("a");

System.out.println (Strarray);

The result is: 1 2 3 A

To delete an element:
Returns whether deleted: List.remove (e);
Delete the element that specifies the subscript directly (delete only the first matching element found): List.remove (index);

Replace element (replaces the element with the specified subscript): List.set (index,e);

remove element: List.get (index);

Empty collection: List.clear ();

Determines whether an element exists in the collection (there is a return of true, there is no return false): List.contains (e);

Compare all the elements in two collections:
Two objects must be equal: List.equals (LIST2);
Two objects are not necessarily equal: list.hashcode () = = List2.hashcode ();
(The Equals method of two equal objects must be true, but two hashcode equal objects are not necessarily equal.) )

Get the element subscript:
The element exists then returns the subscript of the first element found, and returns -1:list.indexof (e) If it does not exist;
The element exists then returns the subscript of the last element found, and returns -1:list.lastindexof (e) If it does not exist;

Determines whether the collection is empty (null returns TRUE, non-null returns FALSE): List.isEmpty ();

Returns the Iterator Collection object: List.iterator ();

Converts a collection to a string: list.tostring ();

Intercept collection (starting from FromIndex in front of Toindex, [Fromindex,toindex)): List.sublist (Fromindex,toindex);

To convert a collection to an array:
Default type: List.toarray ();

Java------List Usage

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.