目录
一、List框架
/----Collection接口:单列集合,用来存储的一个一个的对象 /----List接口:存储有序的,可重复的数据(动态数组) 用来替换原有的数组 /----ArrayList:作为List接口的主要实现类,线程是不安全的,效率高,底层使用Object[] elementDate存储 /----LinkedList:对于频繁的插入、删除操作,效率比ArrayList要高,底层使用的是双向链表 /----Vector:作为List接口的古老实现类,线程安全,效率低,底层使用Object[] elementDate存储
二、List中的常用方法
void add(int index, object ele):在index位置插入ele元素 boolean addAlL(int index, collection eles):从index位置开始将eLes中的所有元素添加进来 object get(int index)∶获取指定index位置的元素 int indexof(object obj):返回obj在集合中首次出现的位置 int lastIndexof(object obj):返回obj在当前集合中末次出现的位置 object remove(int index):移除指定index位置的元素,并返回此元素 object set(int index,objectele):设置指定index位置的元素为eLe List sublist(int fromIndex, int toIndex):返回从fromIndex到toIndex位置的子集合
总结:常用方法 增: add(object obj) 删: remove(int index) / remove(0bject obj) 改: set(int index, object ele) 查: get(int index) 插: add(int index,object ele) 长度:size() 遍历:Iterator迭代器方式:先用iterator()生成一个类似指针的东西,while(iterator.hasNext){输出next()} 增强for循环:for(类型 局部变量:要遍历的数组或集合) 普通的循环:结合get方法
public class ListTest {
@Test
public void test() {
ArrayList list = new ArrayList();
list.add(123);
list.add(345);
list.add("aa");
list.add(new Person(12, "tom"));
List integers = Arrays.asList(1, 23, 4);
list.add(integers);//[123, 345, aa, Person{age=12, name='tom'}, [1, 23, 4]]
//上面是addAll的话,1,23,4就是三个数据
System.out.println(list);
}
}