[Java class set] _ collections list class notes
Objectives of this chapter:
Understanding the relationship between the listlist interface and the list Interface
Measure the test taker's knowledge about the functions of the queue interface.
Struct list subclass and queue Interface
The operation class of a linked list is defined as follows:
Public class Sequence List <E> extends abstractsequentiallist <E> implements list <E>, queue <E>, cloneable, serializable
Queue is a sub-interface of the collection interface, which is defined as follows:
Public interface queue <E> extends collection <E>
Queue interface definition method:
No. method type description
1 Boolean add (e) inserts the specified element into the end of the linked list (if it is feasible immediately and does not violate the Capacity Limit). If it succeeds, true is returned. If there is no available space, illegalstateexception is thrown.
2 E element () is used to obtain the table header of the linked list, but the header of this queue is not removed.
3 Boolean offer (e) inserts the specified element into the end of the linked list (if it is feasible immediately and does not violate the Capacity Limit). When a queue with a capacity limit is used, this method is generally better than add (e). The latter may not be able to insert elements, but only throws an exception.
4 e peek () is normal, but the header of this queue is not removed. If this queue is empty, null is returned.
5 e poll () is used to obtain and remove the headers of this queue. If this queue is empty, null is returned.
6 E remove (): Obtain and remove the queue headers.
Partial Method for operating the linked list in the linked list
No. method type description
1 Public void addfirst (e o) add elements at the beginning of the linked list
2 Public void addlast (e o) add elements to the end of the linked list
3 Public Boolean offer (e o) adds the specified element to the end of the linked list.
4 Public E removefirst (): Delete the first element of the linked list
5 Public E removelast (): Delete the last element of the linked list
The queue list itself greatly extends the operations of the queue interface and list interface. Therefore, it is best to use the sort list class directly to complete the operation during use.
Add data to the beginning and end of the linked list (use the method provided by yourself)
Import Java. util. shortlist; public class extends listdemo01 {public static void main (string ARGs []) {shortlist <string> link = new shortlist <string> (); Link. add ("A"); Link. add ("B"); Link. add ("C"); system. out. println ("initialize linked list:" + link); Link. addfirst ("X"); Link. addlast ("Y"); system. out. println ("add head and tail linked list:" + link );}}
Output:
Initialize the linked list: [a, B, c]
Add the linked list after the header and tail: [X, A, B, C, y]
You can also find the table header of the linked list (the method provided by queue)
Import Java. util. shortlist; public class extends listdemo02 {public static void main (string ARGs []) {shortlist <string> link = new shortlist <string> (); Link. add ("A"); Link. add ("B"); Link. add ("C"); system. out. println ("1-1, element () method to find the header" + link. element (); system. out. println ("1-2. The linked list content after searching:" + link); system. out. println ("1-3, peek () method to find the header:" + link. peek (); system. out. println ("1-4. The linked list content after searching:" + link); system. out. println ("1-5, Poll () method to find the header:" + link. poll () system. out. println ("1-6. The linked list content after searching:" + link );}}
Output:
1-1, element () method to find the header
1-2. The linked list content after searching: [a, B, c]
1-3. Find the header of the peek () method:
1-4. The linked list content after searching: [a, B, c]
1-5, Poll () method to find the header:
1-6. The linked list content after searching: [B, c]