概述
ArrayList与LinkedList均实现List接口,两者有很多相同的方法,其中LinkedList中方法更多,表现为LinkedList具有一些独特的方法,可用作栈和队列的一种实现
主要内容
1. ArrayList与LinkedList转换
2. LinkedList和ArrayList一些相同的方法
3. LinkedList中不同于ArrayList的方法
附录:官方文档中ArrayList和LinkedList的参考资料
详细内容
1. ArrayList与LinkedList转换
// ArrayList与LinkedList之间的转换,一种通过构造方法,另一种通过addAll方法
public static void listConvert() {
List<Integer> arr1 = new ArrayList<Integer>(Arrays.asList(10,56,200,100,99999,6676,6789));
arr1.forEach(o->System.out.print(o+" "));
System.out.println();
Collections.addAll(arr1);
// ArrayList转换为LinkedList
LinkedList<Integer> arr2 = new LinkedList<Integer>(arr1);
arr2.forEach(o->System.out.print(o+" "));
System.out.println();
List<Integer> arr3 = new ArrayList<Integer>();
// LinkedList转为ArrayList
arr3.addAll(arr2);
arr3.forEach(o->System.out.print(o+" "));
System.out.println();
}
2. LinkedList和ArrayList一些相同的方法
// LinkedList和ArrayList,具有大部分相同的属性和方法
public static void listTest() {
// List<Integer> lst = new LinkedList<Integer>(Arrays.asList(0,1,2,3,4,5,6,7));
List<Integer> lst = new ArrayList<Integer>(Arrays.asList(0,1,2,3,4,5,6,7));
// 增加元素
lst.add(30);
// 指定下标之增加水元素
lst.add(0,-1);
lst.add(2);
// -1 0 1 2 3 4 5 6 7 30 2
lst.forEach(o->System.out.print(o+" "));
System.out.println();
// 查找元素
boolean b = lst.contains(7);
// true
System.out.println(b);
// 获取指定位置的元素
Integer x = lst.get(9);
// 30
System.out.println(x);
// 查找指定值首次出现的位置,找不到返回 -1
int cnt = lst.indexOf(2);
// 3
System.out.println(cnt);
// 查找最后出现的位置
int cnt2 = lst.lastIndexOf(2);
// 20
System.out.println(cnt2);
Integer mi = Collections.min(lst);
Integer mx = Collections.max(lst);
// 查找最大值与最小值
System.out.println(mi+" "+mx);
// 修改元素
lst.set(0, -100);
lst.forEach(o->System.out.print(o+" "));
System.out.println();
// 删除元素,元素不存在则抛出异常
Integer del = lst.remove(0);
// -100
System.out.println(del);
lst.forEach(o->System.out.print(o+" "));
// 0 1 2 3 4 5 6 7 30 2
System.out.println();
lst.removeIf(new Predicate<Integer>() {
@Override
public boolean test(Integer t) {
// TODO Auto-generated method stub
return t==30;
// return lst.contains(30);
}});
lst.forEach(o->System.out.print(o+" "));
// 0 1 2 3 4 5 6 7 2
System.out.println();
// lambda表达式判断
boolean res = lst.removeIf((o)->{return o==30;});
// false
System.out.println(res);
// 元素排序
lst.sort((o1,o2)->{return o2-o1;});
lst.forEach(o->System.out.print(o+" "));
// 7 6 5 4 3 2 2 1 0
System.out.println();
// 清空
lst.clear();
}
3. LinkedList中不同于ArrayList的方法
// LinkedList中不同于ArrayList的方法
public static void linkedListTest() {
LinkedList<String> lst = new LinkedList<String>(Arrays.asList("orange","banana","apple","pear","pineapple"));
// add默认添加到最后
// lst.add("peach");
lst.addLast("peach");
// 添加到最前面
lst.addFirst("cherry");
// 添加到最前面
lst.push("watermelon");
lst.forEach(o->System.out.print(o+" "));
// watermelon cherry orange banana apple pear pineapple peach
System.out.println();
// 最前面元素
// watermelon
System.out.println(lst.peek());
// watermelon
System.out.println(lst.peekFirst());
// watermelon
System.out.println(lst.getFirst());
// watermelon
System.out.println(lst.element());
// 最后面的元素
// peach
System.out.println(lst.getLast());
// 删除最前面的元素,没有则抛出异常
// watermelon
System.out.println(lst.pop());
// cherry
// 删除最前面的元素,没有则返回null
System.out.println(lst.poll());
}
官方文档中ArrayList和LinkedList的参考资料
ArrayList构造方法和说明
|
|
|
ArrayList方法摘要
修饰符和类型 | 方法和说明 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LinkedList构造方法和说明
|
|
LinkedList方法摘要
Modifier and Type | Method and Description |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|