Linear table (list)

Source: Internet
Author: User

1. List

The list interface is a collection sub-interface, and list is a repeatable collection

2, ArrayList and LinkedList

ArrayList and LinkedList are two of the most common implementation classes of the list interface, and the list interface is implemented by using dynamic arrays and linked lists respectively.

3. Get and Set methods

In addition to inheriting the method defined by collection, list defines a series of methods based on the data structure of its linear table, the most commonly used is the get and set method based on subscript.

E get (int index): Gets the element that corresponds to the specified subscript in the collection, starting with the subscript 0.

e Set (int index, E elment): Places the given element in the given position and returns the element of the original position.

1List list=NewArrayList ();2List.add ("Java");3List.add ("C #");4List.add ("JAVASCRITP");5List.add ("VBScript");6 //iterating through the collection7  for(intI=0;i<list.size (); i++){8 System.out.println (List.get (i));9 }TenSystem.out.println ("============================"); One //swap positions for elements 2 and 3 AList.set (2, List.set (1, List.get (2)) ) ; -  for(intI=0;i<list.size (); i++){ - System.out.println (List.get (i)); the } -  -  - Results + Java - C # + JAVASCRITP A VBScript at============================ - Java - JAVASCRITP - C # -VBScript

3. Inserting and deleting

The list also supports insert and delete operations based on the subscript:

void Add (int index,e Element):

Inserts the given element into the specified position, and the original position and subsequent elements are moved backwards in order.

E Remove (int index):

Deletes the element at the given position and returns the element that was deleted.

4. Sublist method

The Sublist method of the list is used to get the sub list.

It is important to note that the list obtained by sublist occupies the same storage space as the original list, and the operation of the sub list affects the original list.

list<e> sublist (int fromIndex, int toindex);

Fromindex and Toindex are the subscript (formerly included, not included) of the Intercept sub list.

5. List conversions to arrays

 The ToArray method of the list is used to convert the collection to an array. But the method is actually defined in collection, so all the collections have this function.

There are two ways of it:

  Object[] toArray()

<t>t[] toArray(T[] a)

1 list<string> list=new arraylist<string>(); 2 list.add ("Java"); 3 List.add ("C #"); 4 list.add ("JAVASCRITP"); 5 List.add ("VBScript"); 6 7 object[] arr= list.toarray (); 8 9 string[] arr2 = List.toarray (new string[]{});

The second method is more commonly used, and we can pass in an array of the specified type, and the element type of the array should match the element type of the collection. The return value is the converted array that holds all the elements in the collection.

6. Array conversion to List

  A static method Aslist is provided in the arrays class, which allows us to convert an array to the corresponding list collection. The method is defined as:

Static <T>List<T> aslist<t ... a> the collection element type of the returned List is determined by the element type of the incoming array.

  It is important to note that the returned collection cannot be deleted or not, or it will throw an exception. and modifications to the elements of the collection affect the corresponding elements of the array.

Linear table (list)

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.