Java-a list interface

Source: Internet
Author: User

List Interface

We have mastered the use of the collection interface, and then look at the sub-class in the collection interface, they all have those features?

Next, we'll learn about several commonly used subclasses in collection (list collection ,set collection ).

List Interface Introduction

Check out the API to see the list's introduction. An ordered collection (also known as a sequence). Users of this interface can precisely control the insertion position of each element in the list. The user can access the element based on the integer index of the element (where it is located in the list) and search for the elements in the list. Unlike set, a list usually allows repeating elements.

After reading the API, let's summarize:

List interface:

L It is a collection of elements that are accessed in an orderly manner . For example, the order in which elements are stored is 11, 22, 33. In the collection, the storage of the elements is done in the order of 11, 22, and 33.

L It is an indexed collection that allows you to precisely manipulate the elements in the collection (as with the index of an array).

L can have duplicate elements in the collection, by means of the Equals method of the element, to compare whether it is a repeating element.

Common subclasses of the list interface are:

ArrayList Collection

LinkedList Collection

common methods in the list interface

add Element method

Add (Object e): Adds the specified element at the end of the collection

Add (int index, Object e): Adds the specified element to the collection at the specified index, and the original element moves back

Delete element Delete

Remove (Object e): Removes the specified element object from the collection, returning a value of Boolean

Remove (int index): Removes the element at the specified index from the collection, with the returned value being the deleted element

Replace element method

Set (int index, Object e): Replaces the element at the specified index with the specified element, and returns the value of the element that existed before the replacement

querying Element methods

get (int index): Gets the element at the specified index and returns the element

Method Demo:

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

1, add the element.

List.add ("Little Red");

List.add ("Xiao Mei");

List.add ("Xiao Qiang");

2, insert element. The collection before inserting the element ["Xiao Hong", "Xiao Mei", "Xiao Qiang"]

List.add (1, "Lao Wang"); The collection after inserting the element ["Xiao Hong", "Lao Wang", "Xiao Mei", "Xiao Qiang"]

3. Delete the element.

List.remove (2);//delete the element after the collection ["Little Red", "Lao Wang", "Xiao Qiang"]

4. Modify the element.

List.set (1, "The Old King Next Door");//Modify the elements after the collection ["Little Red", "the Old king next Door", "Xiao Qiang"]

Iterator<string> it = List.iterator ();

while (It.hasnext ()) {

String str = It.next ();

System. out. println (str);

}

Because the list collection has indexes, the list collection iterations can be iterated using an index in addition to using iterators.

for (int i = 0; i < list.size (); i++) {

String str = list.get (i);

System. out. println (str);

}

Concurrency modification exceptions for iterator

In the list collection iteration element, the element is judged and a new element is added once the condition is satisfied. The code is as follows

Public class Iteratordemo {

In the list collection iteration element, the element is judged and a new element is added once the condition is met

Public Static void Main (string[] args) {

Create a list collection

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

Adding elements to the collection

List.add ("ABC1");

List.add ("ABC2");

List.add ("ABC3");

List.add ("ABC4");

Iteration set, when there is an element of "ABC2", the collection joins the new element "a"

Iterator<string> it = List.iterator ();

while (It.hasnext ()) {

String str = It.next ();

Determines whether the removed element is "ABC2" and adds a new element

if ("ABC2". Equals (str)) {

List.add ("a");//The operation will cause an error in the program

}

}

Printing elements in a container

System. out. println (list);

}

}

There was an error running the above code java.util.ConcurrentModificationException[L1] What is the reason?

In the iterative process, the elements are manipulated using the methods of the collection. Causes iterators to not know the changes in the collection, which can easily lead to the uncertainty of the data.

Concurrency Modification Exception Workaround: Do not use the method of the collection to manipulate elements when iterating.

So what do you want to do with the elements during the iteration? It is possible to manipulate elements through the listiterator iterator, listiterator, which resolves the error conditions that may occur during the use of iterator iterations.

the structure of the list collection to store data

There are a number of collections under the list interface, and the structure in which they are stored is different, resulting in these sets having their own characteristics that can be used in different contexts. The common structures of data storage are: stacks, queues, arrays, linked lists . Let's take a look at each:

stack , using the structure of the collection, the access to the element is like the following characteristics:

N Advanced-out (that is, the element to be stored in, after which the elements are removed in turn to remove the element). For example, a bullet is pressed into a magazine, a bullet that is pressed into it, a bullet in the back, a shot in the top, and a bullet that pops up before it pops up.

n the stack's entrance and exit are the top positions of the stack.

N-Stack: The element is stored. That is, the element is stored at the top of the stack, and the elements in the stack move one position to the bottom of the stack in turn.

n the stack: the element is taken. That is, the top position of the stack element is taken out, the stack of elements in turn to the top of the stack to move a position.

L Queue , using the structure of the collection, the access to the element is like the following characteristics:

N FIFO (that is, the element that is stored in, to be removed after the element in front of it, to remove the element). For example, security. In a row, each person checks in turn, only the front of the people to complete the inspection, before the current people to check.

L Arrays , using the structure of the collection, the access to the element is like the following characteristics:

N Find element fast : By index, you can quickly access the element at the specified position

n Adding or deleting elements slow : The reason is that the length is not variable

linked list , using the structure of the collection, the access to the element is like the following characteristics:

n multiple nodes, connected by an address. For example, many people hold hands, everyone uses their right hand to pull down a person's left hand, and so on, so many people are connected together.

n Find element slow : To find an element, you need to go through the connected node, looking back to the specified element

n Adding or deleting elements fast :

ArrayList Collection

The structure of the ArrayList collection data store is the array structure. Elements are slow to delete, Find Fast, because the most used features in daily development for querying data, traversing data, so ArrayList is the most common collection.

Many programmers develop very casually using ArrayList to complete any needs, not rigorous, this usage is not advocated.

LinkedList Collection

The structure of the LinkedList collection data store is the linked list structure. A collection of convenient elements to add and delete. The addition and deletion of a collection element in real-world development often involves end-to-end operations, while LinkedList provides a large number of methods for end-to-end operations.

LinkedList is a sub-class list, the list of methods LinkedList can be used, here does not do a detailed introduction, we only need to understand the unique method of LinkedList. At development time, the LinkedList collection can also be used as a stack, the structure of the queue.

Method Demo:

linkedlist<string> link = new linkedlist<string> ();

adding elements

Link. AddFirst ("ABC1");

Link. AddLast ("ABC2");

Link.addfirst ("ABC3");

get Element

System. out. println (Link.getfirst ());

System. out. println (link.getlast ());

Delete Element

System. out. println (link. Removefirst ());

System. out. println (link. Removelast ());

while (!link. isEmpty ()) {// determine if the collection is empty

System. out. println (Link.pop ()); The top element of the stack in the popup collection

}

Vector Collection

The structure of the vector collection data store is the array structure, which is the oldest provided collection in the JDK. The vector provides a unique way to take out, which is to enumerate enumeration, which is actually an early iterator. The functionality of this interface enumeration is similar to the functionality of the Iterator interface. The vector collection has been replaced by ArrayList. Enumeration enumeration has been replaced by Iterators iterator.

L Vector Common methods:

L Enumeration Enumeration Common methods:

Comparison of the use of vector sets for ArrayList sets

Java-a list interface

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.