java List用法

List包括List接口以及List接口的所有實現類。因爲List接口實現了Collection接口,所以List接口擁有Collection接口提供的所有常用方法,又因爲List是列表類型,所以List接口還提供了一些適合於自身的常用方法,如表1所示。

表1 List接口定義的常用方法及功能
從表1可以看出,List接口提供的適合於自身的常用方法均與索引有關,這是因爲List集合爲列表類型,以線性方式存儲對象,可以通過對象的索引操作對象。
List接口的常用實現類有ArrayList和LinkedList,在使用List集合時,通常情況下聲明爲List類型,實例化時根據實際情況的需要,實例化爲ArrayList或LinkedList,例如:
List<String> l = new ArrayList<String>();// 利用ArrayList類實例化List集合
List<String> l2 = new LinkedList<String>();// 利用LinkedList類實例化List集合
1.add(int index, Object obj)方法和set(int index, Object obj)方法的區別
在使用List集合時需要注意區分add(int index, Object obj)方法和set(int index, Object obj)方法,前者是向指定索引位置添加對象,而後者是修改指定索引位置的對象,例如執行下面的代碼:
src\com\mwq\TestCollection.java關鍵代碼:
public static void main(String[] args) {
String a = "A", b = "B", c = "C", d = "D", e = "E";
List<String> list = new LinkedList<String>();
list.add(a);
list.add(e);
list.add(d);
list.set(1, b);// 將索引位置爲1的對象e修改爲對象b
list.add(2, c);// 將對象c添加到索引位置爲2的位置
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
在控制檯將輸出如下信息:
A
B
C
D
因爲List集合可以通過索引位置訪問對象,所以還可以通過for循環遍歷List集合,例如遍歷上面代碼中的List集合的代碼如下:
src\com\mwq\TestCollection.java關鍵代碼:
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));// 利用get(int index)方法獲得指定索引位置的對象
}
src\com\mwq\TestCollection.java完整代碼如下:
package com.mwq;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.List;
public class TestCollection {
public static void main(String[] args) {
System.out.println("開始:");
String a = "A", b = "B", c = "C", d = "D", e = "E";
List<String> list = new LinkedList<String>();
list.add(a);
list.add(e);
list.add(d);
list.set(1, b);// 將索引位置爲1的對象e修改爲對象b
list.add(2, c);// 將對象c添加到索引位置爲2的位置
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//                 for (int i = 0; i < list.size(); i++) {
//                       System.out.println(list.get(i));// 利用get(int index)方法獲得指定索引位置的對象
//          }
System.out.println("結束!");
}
}
2.indexOf(Object obj)方法和lastIndexOf(Object obj)方法的區別
在使用List集合時需要注意區分indexOf(Object obj)方法和lastIndexOf(Object obj)方法,前者是獲得指定對象的最小的索引位置,而後者是獲得指定對象的最大的索引位置,前提條件是指定的對象在List集合中具有重複的對象,否則如果在List集合中有且僅有一個指定的對象,則通過這兩個方法獲得的索引位置是相同的,例如執行下面的代碼:
src\com\mwq\TestCollection.java關鍵代碼:
public static void main(String[] args) {
String a = "A", b = "B", c = "C", d = "D", repeat = "Repeat";
List<String> list = new ArrayList<String>();
list.add(a);          // 索引位置爲 0
list.add(repeat);      // 索引位置爲 1
list.add(b);          // 索引位置爲 2
list.add(repeat);      // 索引位置爲 3

list.add(c);          // 索引位置爲 4
list.add(repeat);      // 索引位置爲 5
list.add(d);          // 索引位置爲 6
System.out.println(list.indexOf(repeat));
System.out.println(list.lastIndexOf(repeat));
System.out.println(list.indexOf(b));
System.out.println(list.lastIndexOf(b));
}
src\com\mwq\TestCollection.java完整代碼如下:
package com.mwq;
import java.util.ArrayList;
import java.util.List;
public class TestCollection {
public static void main(String[] args) {
System.out.println("開始:");
String a = "A", b = "B", c = "C", d = "D", repeat = "Repeat";
List<String> list = new ArrayList<String>();
list.add(a); // 索引位置爲 0
list.add(repeat); // 索引位置爲 1
list.add(b); // 索引位置爲 2
list.add(repeat); // 索引位置爲 3
list.add(c); // 索引位置爲 4
list.add(repeat); // 索引位置爲 5
list.add(d); // 索引位置爲 6
System.out.println(list.indexOf(repeat));
System.out.println(list.lastIndexOf(repeat));
System.out.println(list.indexOf(b));
System.out.println(list.lastIndexOf(b));
System.out.println("結束!");
}
}
在控制檯將輸出如下信息:
1
5
2
2
3.subList(int fromIndex, int toIndex)方法
在使用subList(int fromIndex, int toIndex)方法截取現有List集合中的部分對象生成新的List集合時,需要注意的是,新生成的集合中包含起始索引位置代表的對象,但是不包含終止索引位置代表的對象,例如執行下面的代碼:
src\com\mwq\TestCollection.java關鍵代碼:
public static void main(String[] args) {
String a = "A", b = "B", c = "C", d = "D", e = "E";
List<String> list = new ArrayList<String>();
list.add(a);          // 索引位置爲 0
list.add(b);          // 索引位置爲 1
list.add(c);          // 索引位置爲 2
list.add(d);          // 索引位置爲 3
list.add(e);          // 索引位置爲 4
list = list.subList(1, 3);// 利用從索引位置 1 到 3 的對象重新生成一個List集合
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
src\com\mwq\TestCollection.java完整代碼:
package com.mwq;
import java.util.ArrayList;
import java.util.List;
public class TestCollection {
public static void main(String[] args) {
System.out.println("開始:");
String a = "A", b = "B", c = "C", d = "D", e = "E";
List<String> list = new ArrayList<String>();
list.add(a); // 索引位置爲 0
list.add(b); // 索引位置爲 1
list.add(c); // 索引位置爲 2
list.add(d); // 索引位置爲 3
list.add(e); // 索引位置爲 4
list = list.subList(1, 3);// 利用從索引位置 1 到 3 的對象重新生成一個List集合
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("結束!");
}
}
在控制檯將輸出如下信息:
B
C

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章