Set operation--linear table

Source: Internet
Author: User

1.ArrayList and LinkedList
The list interface is a sub-interface of collection, which defines a linear table data structure.
The list can be understood as an array of objects, except that the number of elements can be dynamically increased or decreased.
The two common implementation classes of the list interface are ArrayList and LinkedList, and the list interface is implemented by dynamic arrays and linked lists, respectively.
It can be thought that the methods of ArrayList and LinkedList are exactly the same in logic, but there are some differences in performance.
ArrayList is more suitable for random access and LinkedList is more suitable for insertions and deletions.
This difference can be ignored in situations where performance requirements are not particularly harsh.
2.get and set
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 from the original position
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
The Sublist method of the list is used to get the sub list.
It is important to note that sublist gets a list with 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 top and bottom subscripts of the Intercept sub list (formerly included, not included)
5.List conversion to an array
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)
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 type of array element passed in.
It is also important to note that we cannot delete elements from the returned collection, otherwise we will throw an exception. and modifying the elements of the collection affects the corresponding elements of the array.
7.collections.sort Method Implementation Sorting
Collections is a collection of tool classes that provide a lot of ways to make it easier for us to manipulate collections, including the Sort method for collection ordering.
The method is defined as:
void sort (list<t> List)
The function of this method is to sort the given set elements naturally.
8.Comparable
The sort method of collection is to sort the collection elements naturally, so there must be a size between the two element objects. How is this size defined?
In fact, a collection element that uses the sort ordering of collection must be an implementation class of the comparable interface, which indicates that its subclasses are comparable,
Because implementing the interface must override the abstract method:
int CompareTo (T t);
This method is used to compare the current object to the given object.
If the current object is greater than the given object, the return value should be an integer of >0.
If it is less than the given object, the return value should be an integer of <0.
Returns 0 if two objects are equal
9.Comparator
Once the Java class implements the comparable interface, its comparative logic has been determined;
If you want to temporarily specify a comparison rule in a sorted operation, you can use the comparator interface callback method.
The comparator interface requires that the implementation class must override its defined methods:
int compare (T o1,t O2)
The return value of the method requires:
If O1>o2, the return value should be >0
If O1<o2, the return value should be <0
If O1=o2, the return value should be 0

Set operation--linear table

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.