The lateral list class is a bidirectional list. Each node in the list contains references to the previous and next elements.
The constructor of the shortlist is as follows:
1. public linked list (): -- generate an empty linked list
2. public jsonlist (Collection col): Copy constructor
We can use the indexOf () or lastIndexOf () method of the sorted list to find the element (when the first matching element is met, the search is stopped, and the index of the element is returned. Therefore, when there are two identical elements in the listing list, using these two methods will only return the first matched element ). Although the sorted list does not support random search like ArrayList, we can still traverse the entire linear table to find the desired elements.
Java. util. LinkedList is a two-way linked list implementation. Therefore, there are two functions that can be used for search operations:
IndexOf () indicates that the search starts from the header of the linear table. When the first matching element is found, the search is stopped and the index of the matching element is returned.
LastIndexOf () indicates that the search starts from the end of the linear table. When the first matching element is found, the search is stopped and the index of the matching element is returned.
Their index values are calculated starting from the header. Therefore, the values returned by using the indexOf () and lastIndexOf () methods for the same element in the comment list are the same. If there are two identical elements in the comment list, the returned results using lastIndexOf () and indexOf () must be different. As mentioned above, the sorted List does not support random search. To search for an element, you need to traverse the entire List. This means that the time complexity (time complexity) of the search is O (n ).
The following is the structure description of the shortlist:
Linked + list + daa + structure + in + Java
Code example:
Import java. util. Collections list;
/**
* Java Program to search an element inside tables list.
* Shortlist doesn' t provide random search and
* Time complexity of searching is O (n)
*
* @ Author java67
*/
Public class extends listsearch {
Public static void main (String args []) {
Partition list <Integer> ints = new partition List <> ();
Ints. Adding (1001 );
Ints. Adding (1002 );
Ints. Adding (1003 );
Ints. Adding (1004 );
Ints. Adding (1005 );
Ints. Adding (1003 );
// Let's search a duplicate element in linked list
// For duplicate elements indexOf () and lastIndexOf () will
// Return different indexes.
System. out. println ("First index of 1003 is:" + ints. indexOf (1003 ));
System. out. println ("Last index of 1003 is:" + ints. lastIndexOf (1003 ));
// Let's search an element which is not appeared twice
// For unique elements both indexOf () and lastIndexOf () will return
// Same position
System. out. println ("First index of 1002 is:" + ints. indexOf (1002 ));
System. out. println ("Last index of 1002 is:" + ints. lastIndexOf (1002 ));
}
}
Output:
First index of 1003 is: 2
Last index of 1003 is: 5
First index of 1002 is: 1
Last index of 1002 is: 1