list集合
- 特点
- 有序的集合,存储元素和取出元素的顺序是一样的
- 有索引,包含一些带索引的方法
- 允许重复 的元素
- 注意:
- 操作索引的时候,一定要注意防止索越界异常
- IndexOutOfBoundsExeception:索引越界异常,集合会报
- ArrayInsexOutOfBoundsExeception:数组索引引起越界异常
- StringIndexOutOfBoundsExeception:字符串索引越界异常
- 添加元素
list.add(index:X,element:"需要替换的值")
String removeE = List.remove(index:X);
String setE = list.set(X,"需要替换的值");
集合循环方法
Iterator<String> it = list.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
for(String s:list){
System.out.println(s);
}
ArrayList集合
- 实现不是同步的,是多线程,效率高,速度快
- 底层是数组结构:
LinkedList集合
- list接口的链表实现
- 特点:
- 里面包含大量 操作首位元素的方法
- 注意:
- 使用LinkedList集合特有的方法,不能使用多态
- 创建LinkedList集合对象
LinkedList<String> linked = new LinkedList();
linked.add("X");
linked.push("X");//将指定元素插入到列表的开头
linked.addlLast("X");//将指定元素插入到列表的结尾
//二者作用一样
linked.getFirst();
linked.getLast();
//获取第一个和获取最后一个
removeFirst();
removeLast();
pop();