List常用且独特的方法: void add(int index, Object element) 指定下标加元素;add()默认集合末尾加元素 Object set(int index, Object element) 指定下标改元素; Object get(int index) 获取下标元素 int indexOf(Object o) 获取集合第一次出现o的下标 int lastIndexOf(Object o) 获取集合最后一次出现o的下标 Object remove(int index) 删除下标为index的元素
源码:
public class ListText { public static void main(String[] args) { List l1 = new LinkedList(); List l2 = new Vector(); List l3 = new ArrayList(); l3.add(",happy "); l3.add("new "); l3.add("years !"); l3.add(0,"马克"); Iterator it1 = l3.iterator(); while (it1.hasNext()){ Object obj1 = it1.next(); System.out.print(obj1); } System.out.println(); System.out.println(l3.get(0)+"?"); l3.add(",happy "); System.out.println(l3.indexOf(",happy ")); System.out.println(l3.lastIndexOf(",happy ")); l3.remove(4); //删除下标为4的元素 Iterator it2 = l3.iterator(); while (it2.hasNext()){ Object obj2 = it2.next(); System.out.print(obj2); } System.out.println(); l3.set(3,"years!!"); Iterator it3 = l3.iterator(); while (it3.hasNext()){ Object obj3 = it3.next(); System.out.print(obj3); } } }
学习笔记