Generic list & lt; E & gt; generic class

Source: Internet
Author: User

You can use the shortlist <E> generic class to create data objects in the linked list structure. A linked list is a data structure composed of several nodes. Each node contains one data and the reference of the next node (single-chain table ), or contain a data, a reference from the previous node, and a reference from the next node (double-stranded table). The index of the node starts from 0. Linked lists are suitable for dynamically changing the stored data, such as adding or deleting nodes.

1. Upload list <E> Object

Objects Created by the generic list <E> class in the java. util package store data in the linked list structure. It is often called the objects created by the generic list class as Linked List objects. For example:

Mylist. add ("How ");
Mylist. add ("Are ");
Mylist. add ("You ");
Mylist. add ("Java ");

In this case, the double-chain table mylist has four nodes, which are automatically connected and do not need to be connected. That is to say, we do not need to operate the reference of the next or previous node stored in the Arrangement node.

2. Common Methods

Below are some common methods for generic classes in the shortlist <E>.
① Public boolean add (E element) -- add a new node to the end of the linked list. The data in this node is the object specified by the element parameter.
② Public void add (int index, E element) -- add a new node to the specified position of the linked list. The data in this node is the object specified by the element parameter.
③ Public void addFirst (E element) -- Add a new node to the head of the linked list. The data in this node is the object specified by the element parameter.
④ Public void addLast (E element) -- Add a new node to the end of the linked list. The data in this node is the object specified by the element parameter.
⑤ Public void clear () -- delete all nodes in the linked list to make the current linked list empty.
⑥ Public E remove (int index) -- delete a node at a specified position.
7. public boolean remove (E element) -- delete the node with data element for the first time.
Revoke public E removeFirst () -- Delete the first node and return the objects in the node.
Revoke public E removeLast () -- Delete the last Node object and return the object of this node.
Except public E get (int index) -- get the object in the node at the specified position in the linked list.
Revoke public E getFirst () -- get the object in the first node in the linked list.
Inclupublic E getLast () -- get the object in the last node in the linked list.
Except public int indexOf (E element) -- returns the first position of the node containing the data element in the linked list. If this node is not found in the linked list,-1 is returned.
Inclupublic int lastIndexOf (E element) -- returns the last position of the node containing the data element in the linked list. If this node is not found in the linked list,-1 is returned.
Except public E set (int index, E element) -- replace the object element in the index position node of the current linked list with the object specified by the element parameter, and return the replaced object.
Returns the length of the linked list, that is, the number of nodes.
Extends public boolean contains (Object element) -- determines whether a node in the linked list contains an Object element.
Revoke public Object clone () -- get a clone linked list of the current linked list. Changing the node data in the clone linked list will not affect the node data in the current linked list, and vice versa.

 

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.