The ArrayList class supports retrieval of a single element by index with 'public Object get(int index)'.
import java.util.ArrayList;
import java.util.List;
public class MainClass {
public static void main(String args[]) throws Exception {
List list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list.get(2));
}
}
运行结果如下:
Get Sub List of ArrayList
import java.util.ArrayList;
import java.util.List;
/*from ww w.j av a2 s . c o m*/
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
List lst = arrayList.subList(1, 3);
for (int i = 0; i < lst.size(); i++)
System.out.println(lst.get(i));
// remove one element from sub list
Object obj = lst.remove(0);
System.out.println(obj + " is removed");
for (String str: arrayList)
System.out.println(str);
}
}