java--(v) Collection list collection, ArrayList and vector implementation classes

Source: Internet
Author: User
Tags object object set set

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

First, List collection

The list collection represents an ordered, guest-repeating collection of elements in which each element has its corresponding sequential index. List

Collections allow you to use duplicate elements, which can be indexed to access the collection elements at the specified location. The list collection is added by element by default

The index of the sequential set element, such as the first time the element index is 0, the second time the element index is 1 ...

1.List interface and Listiterator interface

List as a sub-interface of the collection interface, so you can use all the methods in the collection interface. and list

is an ordered set, the list collection adds some methods to manipulate the collection elements according to the index.

1) void Add (int index, Object Element): Elements element is inserted at index of the list collection.

2) Boolean addall (int index, Collection c): Inserts all the elements contained in collection C into the list collection's

At index.

3) Object get (int index): Returns the element at the index of the collection index.

4) int IndexOf (object o): Returns the position index of the first occurrence of the object o in the list collection.

5) int LastIndexOf (object o): Returns the position index of the last occurrence of the object o in the list collection.

6) obejct Remove (int index): deletes and returns the element at index.
7) object Set (int index, Object Element): Replaces the element at index

Back to the new element.

8) List sublist (int fromIndex, int toindex): Returns from index FromIndex (inclusive) to index Toindex

(not included) a subset of the opportunity elements in the premises.

All of the list implementation classes can invoke these methods to manipulate the collection elements. Compared to the set set, the list adds an index to the

Methods for entering, replacing, and deleting collection elements. The following program demonstrates the general use of the list collection.

1 Importjava.util.ArrayList;2 Importjava.util.List;3 4 5  Public classListtest {6 7      Public Static voidMain (string[] args) {8 9List test =NewArraylist<>();Ten         //adding three elements to a test collection OneTest.add (NewString ("Dark Horse Programmer")); ATest.add (NewString ("Preach Intelligence podcast")); -Test.add (NewString ("CSDN")); - System.out.println (test); the         //inserts a new string object in the second position -Test.add (1,NewString ("Preach Intelligence podcast")); -         /*For (Object object:test) { - System.out.println (object); +         }*/ -          for(inti = 0; I < test.size (); i++) { + System.out.println (Test.get (i)); A         } at         //Delete the third element -Test.remove (2); - System.out.println (test); -         //determines the position of the specified element in the list collection, Output 2, indicating the unique second bit -System.out.println (Test.indexof (NewString ("CSDN"))); -         //Replace the second element with a new string object inTest.set (0,NewString ("CSDN")); - System.out.println (test); to         //intercepts the second element of the test collection (contained) to the third element (not included) into a subset +System.out.println (Test.sublist (1, 2)); -     } the  *}

Shipping results:

[Dark Horse programmer, Preach Wisdom Podcast, CSDN] Black Horse programmer preach Wisdom Podcast Biography Intelligence Podcast csdn[Black Horse Programmer, Preach Wisdom Podcast, CSDN]2[CSDN, Preach Wisdom Podcast, csdn][Preach Wisdom Podcast]

2.ArrayList and vector implementation classes

ArrayList and Vector are the two typical implementations of the list class and fully support the full functionality of the list interface described previously.

Both the ArrayList and the vector classes are based on the number list class, and all ArrayList and vectors encapsulate a dynamic,

Object[] array to allow redistribution. The ArrayList or Vector object uses the initalcapacity parameter to set the array's

length, when adding elements to ArrayList or vectors beyond the length of the array, their initalcapacity will be self-

Increase in motion. If the initalcapacity parameter is not specified when creating an empty ArrayList or vector collection, the object[] array

The length defaults to 10.

In addition, ArrayList and Vector also provide the following two ways to reassign the object[] array.

1) void ensurecapacity (int mincapacity): ArrayList or vector set of object[] Array

Increase mincapacity.

2) void TrimToSize (): Adjusts the object[] array length of the ArrayList or vector set to the current element

Number. The program guest calls this method to reduce the storage space occupied by the ArrayList or Vector collection object.

Vector also provides a stack subclass, which is used to simulate the "stack" of this data structure, "stack" usually refers to "LIFO"

(LIFO) of the container. The last "push" element will be the first to be "pop" out of the stack. As with other collections in Java, the input

The stack is a stack of objects, so you must convert the elements after you remove them from the stack. So the Stack class provides the following

A method.

1) Object Peek (): Returns the first element of the stack, but does not stack the element "Pop".

2) Object pop (): Returns the first element of the "stack" and pushes the element "pop" out of the stack.

3) void push (Object Item): Adds an element "push" to the stack, and the last element in the "stack" is always at the top of the stack.

The following program demonstrates vector as a "stack" function.

1 ImportJava.util.Stack;2 3 4  Public classVectortest {5 6      Public Static voidMain (string[] args) {7 8Stack Vstack =NewStack<>();9         //three element "push" in the stack at a timeTenVstack.push ("Dark Horse Programmer"); OneVstack.push ("Preach Intelligence podcast"); AVstack.push ("CSDN"); -          - System.out.println (vstack); the         //accesses the first element, but does not stack its "pop" - System.out.println (Vstack.peek ()); - System.out.println (vstack); -         //"Pop" out of the stack first element + System.out.println (Vstack.pop ()); - System.out.println (vstack); +     } A  at}

Operation Result:

[Dark Horse programmer, Wisdom Podcast, CSDN] csdn[Black Horse programmer, Preach Intelligence podcast, csdn]csdn[Dark Horse programmer, Preach Wisdom Podcast]

Since the stack inherits the vector, it is also a very old Java collection class, it is thread-safe, performance comparison

, so the stack class is generally less commonly used in the current program. If your program needs to use a data structure such as "stack," you can consider making

With LinkedList.

java--(v) Collection list collection, ArrayList and vector implementation classes

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.