The linked list provides a method to use the linked list as a stack, queue, or dual-end queue. Here, we will not repeat the standard list operations, but will only show how to use it.
Note the Implemented interfaces.
Serializable, cloneable, iterable <E>, collection <E>, deque <E>, list <E>, queue <E>
- Package collection. lession9;
- Import java. util. arrays;
- Import java. util. iterator;
- Import java. util. Collections list;
- Import java. util. List;
- /**
- * Tutorial on improving Java in old zizhu (6)-understanding the history list of list lists <br>
- * Two-way queue control, including operations at the beginning and end <br>
- * It Can Be Used for Stack and FIFO operations. <Br>
- * <Br>
- * For more information about APIs, see api.java2000.net/#list.
- *
- * @ Author old zizhu Java century network (java2000.net)
- *
- */
- Public class lession9 {
- Public static void main (string [] ARGs ){
- // Basic list operations, which are not repeated here
- // View the list content
- // Here we focus on the features introduced by the feature list.
- //
- Shortlist <string> List = new shortlist <string> ();
- List. Add ("1111 ");
- List. Add ("2222 ");
- List. Add ("3333 ");
- Showlist (list); // [1111,222 2, 3333]
- // Add one at the top of the list
- List. addfirst ("head ");
- Showlist (list); // [Head, 1111,222 2, 3333]
- // Test the reverse iterator
- // The normal iterator should start with the first data
- // This is from the last one, which is also a feature of the listing list
- Iterator <string> it = List. descendingiterator ();
- While (it. hasnext ()){
- System. Out. Print (it. Next () + ","); // 3333,2222, 1111, Head,
- }
- System. Out. println ();
- // Obtain the first data in the list and keep the data.
- // Getfirst is used internally, no difference
- System. Out. println (list. element (); // head
- System. Out. println (list. getfirst (); // head
- // If the length is 0, null is returned; otherwise, it is the same as getfirst.
- System. Out. println (list. Peek (); // head
- System. Out. println (list. peekfirst (); // head
- // Obtain the last data in the list and keep the data.
- System. Out. println (list. getlast (); // 3333
- // If the length is 0, null is returned; otherwise, it is the same as getlast.
- System. Out. println (list. peeklast (); // 3333
- // Add a data entry at the end of the list
- List. Offer ("tail ");
- Showlist (list); // [Head, 1111,222 2, 3333, tail]
- // Add a data entry at the beginning of the List
- // Implemented internally through addfirst
- List. offerfirst ("----");
- Showlist (list); // [----, Head, 1111,222 2, 3333, tail]
- // Add a data entry at the end of the list
- // Implemented internally through addlast
- List. offerlast ("==== ");
- Showlist (list); // [----, Head, 1111,222 2, 3333, tail, ===]
- // Obtain the first data in the list and delete the data from
- System. Out. println (list. Poll ());//----
- System. Out. println (list. pollfirst (); // head
- // Obtain the last data in the list and delete the data
- System. Out. println (list. polllast (); // ====
- // Store a data in the form of a stack
- // Insert the element at the beginning of the List
- // Internal implementation through addfirst
- List. Push ("XYZ ");
- Showlist (list); // [xyz, 1111,222 2, 3333, tail]
- // A data is popped up in the form of a stack.
- // Remove and return the first data
- // Internal implementation through removefirst
- System. Out. println (list. Pop (); // xyz
- Showlist (list); // [1111,222 2, 3333, tail]
- // Delete and return the first data
- System. Out. println (list. removefirst (); // 1111
- Showlist (list); // [2222,333 3, tail]
- // Delete the data that appears for the first time in the list
- // Internal implementation through remove (object)
- System. Out. println (list. removefirstoccurrence ("3333"); // true
- Showlist (list); // [2222, tail]
- // Delete and return the last data
- System. Out. println (list. removelast (); // tail
- Showlist (list); // [2222]
- // Delete the last data in the list
- System. Out. println (list. removelastoccurrence ("2222"); // true
- Showlist (list); // []
- }
- /**
- * Displays the data in the list.
- *
- * @ Param list
- */
- Private Static void showlist (list ){
- System. Out. println (arrays. tostring (list. toarray ()));
- }
- }
Running result
[1111,222, 3333]
[Head, 1111,222 2, 3333]
3333,2222, 1111, Head,
Head
Head
Head
Head
3333
3333
[Head, 1111,222 2, 3333, tail]
[----, Head, 1111,222 2, 3333, tail]
[----, Head, 1111,222 2, 3333, tail, ===]
----
Head
====
[Xyz, 1111,222 2, 3333, tail]
XYZ
[1111,222 2, 3333, tail]
1111
[2222,333 3, tail]
True
[2222, tail]
Tail
[2222]
True
[]
Summary:
This class has great flexibility and is also one of the commonly used List Implementation classes. I will not talk about it anymore. In short, you must master it.