Old zizhu Java tutorial (6)-Understanding list

Source: Internet
Author: User
Tags addall
Address: http://www.java2000.net/p11848
 
List is a frequently-used data structure, which is more frequently used than map and set, because I often use it to return the database operation result set.

  1. Package collection. lession6;
  2. Import java. util. arraylist;
  3. Import java. util. iterator;
  4. Import java. util. Collections list;
  5. Import java. util. List;
  6. Import java. util. listiterator;
  7. Import java. util. Stack;
  8. Import java. util. vector;
  9. /**
  10. * Old zizhu Java tutorial (6)-list <br>
  11. * List can precisely control the sequence in the set. <Br>
  12. * Data can be accessed directly through the data location. <Br>
  13. * Repeated data is generally allowed <br>
  14. * Generally, null is allowed and multiple null values are allowed.
  15. *
  16. * @ Author old zizhu Java century network (java2000.net)
  17. *
  18. */
  19. Public class lession6 {
  20. Public static void main (string [] ARGs ){
  21. // The following are commonly used implementation classes in list
  22. // Implement a variable-size list, allowing any data to be added
  23. Arraylist = new arraylist ();
  24. // It is also the implementation of a variable-size list.
  25. // The biggest difference from arraylist is that the method of this class is synchronous (synchronized ).
  26. // For detailed differences, see
  27. // Check the source code to understand the real difference between arraylist and vector.
  28. // Http://www.java2000.net/p9750
  29. Vector vector = new vector ();
  30. // Implements bidirectional queue control, including operations at the beginning and end
  31. // Can be used for Stack and FIFO operations.
  32. Listparts list = new parts list ();
  33. // Integrate the self-Vector
  34. // Provides the LIFO stack operation method.
  35. Stack stack = new stack ();
  36. //-----------------
  37. // The following uses arraylist as an example to check the methods in the list.
  38. List list = new arraylist ();
  39. // Construct the second list
  40. List list2 = new arraylist ();
  41. // Add the specified data to the end of the list
  42. List. Add (1, 123 );
  43. // Insert the specified data at the specified position in the list
  44. // Insert data at the beginning
  45. // The index at the position of the list starts from 0.
  46. List. Add (1, 0,456 );
  47. // Put all the data in list2 to the end of list1.
  48. // Note that this can be any collection class, not limited to list
  49. List. addall (list2 );
  50. // Put all the data in list2 to the end of the specified position in list1.
  51. // Insert a piece of data
  52. List. addall (2, list2 );
  53. // Clear the list and delete all the data in it
  54. List. Clear ();
  55. // Determine whether the list contains data
  56. Boolean found = List. Contains (123 );
  57. // Determine whether the list contains all the data of another set
  58. // Note that this can be any collection class, not limited to list
  59. Boolean OK = List. containsall (list2 );
  60. // Obtain data at the specified position
  61. // If the position exceeds the range of list 0 to list. Size ()-1
  62. // Throw an exception: indexoutofboundsexception
  63. Object OBJ = list. Get (3 );
  64. // Obtain the position of the data in the list
  65. // If not found,-1 is returned.
  66. // The Position Index starts from 0.
  67. Int Index = List. indexof (456 );
  68. // Check whether the list is empty
  69. Boolean empty = List. isempty ();
  70. // List iterator operation
  71. // The Order is strictly in the order of list Storage
  72. Iterator it = List. iterator ();
  73. // Obtain the position where the data is last displayed in the list
  74. // Suitable for the list with duplicate data
  75. Index = List. lastindexof (456 );
  76. // Another iterator in the list
  77. Listiterator listit = List. listiterator ();
  78. // Another iterator In the list. You can specify the start position.
  79. Listiterator listit2 = List. listiterator (3 );
  80. // Delete data at a certain position
  81. List. Remove (3 );
  82. // Delete the first location where the specified object appears
  83. // Note: if it is an integer, you must distinguish it from remove (INT ).
  84. // We recommend that you use remove (New INTEGER (123); to delete data objects.
  85. List. Remove (New INTEGER (123 ));
  86. // Delete the data in another set in the list
  87. List. removeall (list2 );
  88. // Only the data in another set is retained, equal to the intersection
  89. List. retainall (list2 );
  90. // Replace the data at the specified position
  91. List. Set (3,999 );
  92. // Number of List Data
  93. Int size = List. Size ();
  94. // Obtain a subarray
  95. List sublist = List. sublist (2, 10 );
  96. // Convert the set into an array
  97. Object [] objs = List. toarray ();
  98. // Convert the set to an array in the specified format
  99. // For example, strings are saved in the set.
  100. String [] objs2 = (string []) List. toarray (New String [0]);
  101. }
  102. }

Summary:

In fact, we usually use arraylist. Because vector is a synchronous method, it is used only when multiple threads and global data are involved.

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.