Old zizhu Java tutorial (9)-understanding the history list of list lists

Source: Internet
Author: User
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>

  1. Package collection. lession9;
  2. Import java. util. arrays;
  3. Import java. util. iterator;
  4. Import java. util. Collections list;
  5. Import java. util. List;
  6. /**
  7. * Tutorial on improving Java in old zizhu (6)-understanding the history list of list lists <br>
  8. * Two-way queue control, including operations at the beginning and end <br>
  9. * It Can Be Used for Stack and FIFO operations. <Br>
  10. * <Br>
  11. * For more information about APIs, see api.java2000.net/#list.
  12. *
  13. * @ Author old zizhu Java century network (java2000.net)
  14. *
  15. */
  16. Public class lession9 {
  17. Public static void main (string [] ARGs ){
  18. // Basic list operations, which are not repeated here
  19. // View the list content
  20. // Here we focus on the features introduced by the feature list.
  21. //
  22. Shortlist <string> List = new shortlist <string> ();
  23. List. Add ("1111 ");
  24. List. Add ("2222 ");
  25. List. Add ("3333 ");
  26. Showlist (list); // [1111,222 2, 3333]
  27. // Add one at the top of the list
  28. List. addfirst ("head ");
  29. Showlist (list); // [Head, 1111,222 2, 3333]
  30. // Test the reverse iterator
  31. // The normal iterator should start with the first data
  32. // This is from the last one, which is also a feature of the listing list
  33. Iterator <string> it = List. descendingiterator ();
  34. While (it. hasnext ()){
  35. System. Out. Print (it. Next () + ","); // 3333,2222, 1111, Head,
  36. }
  37. System. Out. println ();
  38. // Obtain the first data in the list and keep the data.
  39. // Getfirst is used internally, no difference
  40. System. Out. println (list. element (); // head
  41. System. Out. println (list. getfirst (); // head
  42. // If the length is 0, null is returned; otherwise, it is the same as getfirst.
  43. System. Out. println (list. Peek (); // head
  44. System. Out. println (list. peekfirst (); // head
  45. // Obtain the last data in the list and keep the data.
  46. System. Out. println (list. getlast (); // 3333
  47. // If the length is 0, null is returned; otherwise, it is the same as getlast.
  48. System. Out. println (list. peeklast (); // 3333
  49. // Add a data entry at the end of the list
  50. List. Offer ("tail ");
  51. Showlist (list); // [Head, 1111,222 2, 3333, tail]
  52. // Add a data entry at the beginning of the List
  53. // Implemented internally through addfirst
  54. List. offerfirst ("----");
  55. Showlist (list); // [----, Head, 1111,222 2, 3333, tail]
  56. // Add a data entry at the end of the list
  57. // Implemented internally through addlast
  58. List. offerlast ("==== ");
  59. Showlist (list); // [----, Head, 1111,222 2, 3333, tail, ===]
  60. // Obtain the first data in the list and delete the data from
  61. System. Out. println (list. Poll ());//----
  62. System. Out. println (list. pollfirst (); // head
  63. // Obtain the last data in the list and delete the data
  64. System. Out. println (list. polllast (); // ====
  65. // Store a data in the form of a stack
  66. // Insert the element at the beginning of the List
  67. // Internal implementation through addfirst
  68. List. Push ("XYZ ");
  69. Showlist (list); // [xyz, 1111,222 2, 3333, tail]
  70. // A data is popped up in the form of a stack.
  71. // Remove and return the first data
  72. // Internal implementation through removefirst
  73. System. Out. println (list. Pop (); // xyz
  74. Showlist (list); // [1111,222 2, 3333, tail]
  75. // Delete and return the first data
  76. System. Out. println (list. removefirst (); // 1111
  77. Showlist (list); // [2222,333 3, tail]
  78. // Delete the data that appears for the first time in the list
  79. // Internal implementation through remove (object)
  80. System. Out. println (list. removefirstoccurrence ("3333"); // true
  81. Showlist (list); // [2222, tail]
  82. // Delete and return the last data
  83. System. Out. println (list. removelast (); // tail
  84. Showlist (list); // [2222]
  85. // Delete the last data in the list
  86. System. Out. println (list. removelastoccurrence ("2222"); // true
  87. Showlist (list); // []
  88. }
  89. /**
  90. * Displays the data in the list.
  91. *
  92. * @ Param list
  93. */
  94. Private Static void showlist (list ){
  95. System. Out. println (arrays. tostring (list. toarray ()));
  96. }
  97. }

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.

Related Article

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.