java List的用法

Java代码 复制代码 收藏代码
  1. java List的用法List的用法   
  2. List包括List接口以及List接口的所有实现类。因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以List接口还提供了一些适合于自身的常用方法,如表1所示。   
  3.   
  4. 1 List接口定义的常用方法及功能   
  5. 从表1可以看出,List接口提供的适合于自身的常用方法均与索引有关,这是因为List集合为列表类型,以线性方式存储对象,可以通过对象的索引操作对象。   
  6. List接口的常用实现类有ArrayList和LinkedList,在使用List集合时,通常情况下声明为List类型,实例化时根据实际情况的需要,实例化为ArrayList或LinkedList,例如:   
  7. List<String> l = new ArrayList<String>();// 利用ArrayList类实例化List集合   
  8. List<String> l2 = new LinkedList<String>();// 利用LinkedList类实例化List集合   
  9. 1.add(int index, Object obj)方法和set(int index, Object obj)方法的区别   
  10. 在使用List集合时需要注意区分add(int index, Object obj)方法和set(int index, Object obj)方法,前者是向指定索引位置添加对象,而后者是修改指定索引位置的对象,例如执行下面的代码:   
  11. src\com\mwq\TestCollection.java关键代码:   
  12. public static void main(String[] args) {   
  13. String a = "A", b = "B", c = "C", d = "D", e = "E";   
  14. List<String> list = new LinkedList<String>();   
  15. list.add(a);   
  16. list.add(e);   
  17. list.add(d);   
  18. list.set(1, b);// 将索引位置为1的对象e修改为对象b   
  19. list.add(2, c);// 将对象c添加到索引位置为2的位置   
  20. Iterator<String> it = list.iterator();   
  21. while (it.hasNext()) {   
  22. System.out.println(it.next());   
  23. }   
  24. }   
  25. 在控制台将输出如下信息:   
  26. A   
  27. B   
  28. C   
  29. D   
  30. 因为List集合可以通过索引位置访问对象,所以还可以通过for循环遍历List集合,例如遍历上面代码中的List集合的代码如下:   
  31. src\com\mwq\TestCollection.java关键代码:   
  32. for (int i = 0; i < list.size(); i++) {   
  33. System.out.println(list.get(i));// 利用get(int index)方法获得指定索引位置的对象   
  34. }   
  35. src\com\mwq\TestCollection.java完整代码如下:   
  36. package com.mwq;   
  37. import java.util.ArrayList;   
  38. import java.util.LinkedList;   
  39. import java.util.Iterator;   
  40. import java.util.List;   
  41. public class TestCollection {   
  42. public static void main(String[] args) {   
  43. System.out.println("开始:");   
  44. String a = "A", b = "B", c = "C", d = "D", e = "E";   
  45. List<String> list = new LinkedList<String>();   
  46. list.add(a);   
  47. list.add(e);   
  48. list.add(d);   
  49. list.set(1, b);// 将索引位置为1的对象e修改为对象b   
  50. list.add(2, c);// 将对象c添加到索引位置为2的位置   
  51. Iterator<String> it = list.iterator();   
  52. while (it.hasNext()) {   
  53. System.out.println(it.next());   
  54. }   
  55. //                 for (int i = 0; i < list.size(); i++) {   
  56. //                       System.out.println(list.get(i));// 利用get(int index)方法获得指定索引位置的对象   
  57. //          }   
  58. System.out.println("结束!");   
  59. }   
  60. }   
  61. 2.indexOf(Object obj)方法和lastIndexOf(Object obj)方法的区别   
  62. 在使用List集合时需要注意区分indexOf(Object obj)方法和lastIndexOf(Object obj)方法,前者是获得指定对象的最小的索引位置,而后者是获得指定对象的最大的索引位置,前提条件是指定的对象在List集合中具有重复的对象,否则如果在List集合中有且仅有一个指定的对象,则通过这两个方法获得的索引位置是相同的,例如执行下面的代码:   
  63. src\com\mwq\TestCollection.java关键代码:   
  64. public static void main(String[] args) {   
  65. String a = "A", b = "B", c = "C", d = "D", repeat = "Repeat";   
  66. List<String> list = new ArrayList<String>();   
  67. list.add(a);          // 索引位置为 0   
  68. list.add(repeat);      // 索引位置为 1   
  69. list.add(b);          // 索引位置为 2   
  70. list.add(repeat);      // 索引位置为 3    
  71.   
  72. list.add(c);          // 索引位置为 4   
  73. list.add(repeat);      // 索引位置为 5   
  74. list.add(d);          // 索引位置为 6   
  75. System.out.println(list.indexOf(repeat));   
  76. System.out.println(list.lastIndexOf(repeat));   
  77. System.out.println(list.indexOf(b));   
  78. System.out.println(list.lastIndexOf(b));   
  79. }   
  80. src\com\mwq\TestCollection.java完整代码如下:   
  81. package com.mwq;   
  82. import java.util.ArrayList;   
  83. import java.util.List;   
  84. public class TestCollection {   
  85. public static void main(String[] args) {   
  86. System.out.println("开始:");   
  87. String a = "A", b = "B", c = "C", d = "D", repeat = "Repeat";   
  88. List<String> list = new ArrayList<String>();   
  89. list.add(a); // 索引位置为 0   
  90. list.add(repeat); // 索引位置为 1   
  91. list.add(b); // 索引位置为 2   
  92. list.add(repeat); // 索引位置为 3   
  93. list.add(c); // 索引位置为 4   
  94. list.add(repeat); // 索引位置为 5   
  95. list.add(d); // 索引位置为 6   
  96. System.out.println(list.indexOf(repeat));   
  97. System.out.println(list.lastIndexOf(repeat));   
  98. System.out.println(list.indexOf(b));   
  99. System.out.println(list.lastIndexOf(b));   
  100. System.out.println("结束!");   
  101. }   
  102. }   
  103. 在控制台将输出如下信息:   
  104. 1  
  105. 5  
  106. 2  
  107. 2  
  108. 3.subList(int fromIndex, int toIndex)方法   
  109. 在使用subList(int fromIndex, int toIndex)方法截取现有List集合中的部分对象生成新的List集合时,需要注意的是,新生成的集合中包含起始索引位置代表的对象,但是不包含终止索引位置代表的对象,例如执行下面的代码:   
  110. src\com\mwq\TestCollection.java关键代码:   
  111. public static void main(String[] args) {   
  112. String a = "A", b = "B", c = "C", d = "D", e = "E";   
  113. List<String> list = new ArrayList<String>();   
  114. list.add(a);          // 索引位置为 0   
  115. list.add(b);          // 索引位置为 1   
  116. list.add(c);          // 索引位置为 2   
  117. list.add(d);          // 索引位置为 3   
  118. list.add(e);          // 索引位置为 4   
  119. list = list.subList(13);// 利用从索引位置 1 到 3 的对象重新生成一个List集合   
  120. for (int i = 0; i < list.size(); i++) {   
  121. System.out.println(list.get(i));   
  122. }   
  123. }   
  124. src\com\mwq\TestCollection.java完整代码:   
  125. package com.mwq;   
  126. import java.util.ArrayList;   
  127. import java.util.List;   
  128. public class TestCollection {   
  129. public static void main(String[] args) {   
  130. System.out.println("开始:");   
  131. String a = "A", b = "B", c = "C", d = "D", e = "E";   
  132. List<String> list = new ArrayList<String>();   
  133. list.add(a); // 索引位置为 0   
  134. list.add(b); // 索引位置为 1   
  135. list.add(c); // 索引位置为 2   
  136. list.add(d); // 索引位置为 3   
  137. list.add(e); // 索引位置为 4   
  138. list = list.subList(13);// 利用从索引位置 1 到 3 的对象重新生成一个List集合   
  139. for (int i = 0; i < list.size(); i++) {   
  140. System.out.println(list.get(i));   
  141. }   
  142. System.out.println("结束!");   
  143. }   
  144. }   
  145. 在控制台将输出如下信息:   
  146. B   
  147. C  

posted on 2013-02-14 19:13  蜜雪薇琪  阅读(178)  评论(0编辑  收藏  举报