概述

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()构造一个初始容量为十的空列表。

ArrayList(Collection<? extends E> c)构造一个包含指定集合的元素的列表,按照它们由集合的迭代器返回的顺序。

ArrayList(int initialCapacity)构造具有指定初始容量的空列表。

ArrayList方法摘要

修饰符和类型

方法和说明

boolean

add(E e)将指定的元素追加到此列表的末尾。

void

add(int index, E element)在此列表中的指定位置插入指定的元素。

boolean

addAll(Collection<? extends E> c)按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾。

boolean

addAll(int index, Collection<? extends E> c)将指定集合中的所有元素插入到此列表中,从指定的位置开始。

void

clear()从列表中删除所有元素。

Object

clone()返回此 ArrayList实例的浅拷贝。

boolean

contains(Object o)如果此列表包含指定的元素,则返回 true

void

ensureCapacity(int minCapacity)如果需要,增加此 ArrayList实例的容量,以确保它可以至少保存最小容量参数指定的元素数。

void

forEach(Consumer<? super E> action)Iterable的每个元素执行给定的操作,直到所有元素都被处理或动作引发异常。

E

get(int index)返回此列表中指定位置的元素。

int

indexOf(Object o)返回此列表中指定元素的第一次出现的索引,如果此列表不包含元素,则返回-1。

boolean

isEmpty()如果此列表不包含元素,则返回 true

Iterator<E>

iterator()以正确的顺序返回该列表中的元素的迭代器。

int

lastIndexOf(Object o)返回此列表中指定元素的最后一次出现的索引,如果此列表不包含元素,则返回-1。

ListIterator<E>

listIterator()返回列表中的列表迭代器(按适当的顺序)。

ListIterator<E>

listIterator(int index)从列表中的指定位置开始,返回列表中的元素(按正确顺序)的列表迭代器。

E

remove(int index)删除该列表中指定位置的元素。

boolean

remove(Object o)从列表中删除指定元素的第一个出现(如果存在)。

boolean

removeAll(Collection<?> c)从此列表中删除指定集合中包含的所有元素。

boolean

removeIf(Predicate<? super E> filter)删除满足给定谓词的此集合的所有元素。

protected void

removeRange(int fromIndex, int toIndex)从这个列表中删除所有索引在 fromIndex (含)和 toIndex之间的元素。

void

replaceAll(UnaryOperator<E> operator)将该列表的每个元素替换为将该运算符应用于该元素的结果。

boolean

retainAll(Collection<?> c)仅保留此列表中包含在指定集合中的元素。

E

set(int index, E element)用指定的元素替换此列表中指定位置的元素。

int

size()返回此列表中的元素数。

void

sort(Comparator<? super E> c)使用提供的 Comparator对此列表进行排序以比较元素。

Spliterator<E>

spliterator()在此列表中的元素上创建late-binding故障快速 Spliterator 。**

List<E>

subList(int fromIndex, int toIndex)返回此列表中指定的 fromIndex (包括)和 toIndex之间的独占视图。

Object[]

toArray()以正确的顺序(从第一个到最后一个元素)返回一个包含此列表中所有元素的数组。

<T> T[]

toArray(T[] a)以正确的顺序返回一个包含此列表中所有元素的数组(从第一个到最后一个元素); 返回的数组的运行时类型是指定数组的运行时类型。

void

trimToSize()修改这个 ArrayList实例的容量是列表的当前大小。

LinkedList构造方法和说明

LinkedList()构造一个空列表。

LinkedList(Collection<? extends E> c)构造一个包含指定集合的元素的列表,按照它们由集合的迭代器返回的顺序。

LinkedList方法摘要

Modifier and Type

Method and Description

boolean

add(E e)将指定的元素追加到此列表的末尾。

void

add(int index, E element)在此列表中的指定位置插入指定的元素。

boolean

addAll(Collection<? extends E> c)按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾。

boolean

addAll(int index, Collection<? extends E> c)将指定集合中的所有元素插入到此列表中,从指定的位置开始。

void

clear()从列表中删除所有元素。

Object

clone()返回此 ArrayList实例的浅拷贝。

boolean

contains(Object o)如果此列表包含指定的元素,则返回 true

void

ensureCapacity(int minCapacity)如果需要,增加此 ArrayList实例的容量,以确保它可以至少保存最小容量参数指定的元素数。

void

forEach(Consumer<? super E> action)Iterable的每个元素执行给定的操作,直到所有元素都被处理或动作引发异常。

E

get(int index)返回此列表中指定位置的元素。

int

indexOf(Object o)返回此列表中指定元素的第一次出现的索引,如果此列表不包含元素,则返回-1。

boolean

isEmpty()如果此列表不包含元素,则返回 true

Iterator<E>

iterator()以正确的顺序返回该列表中的元素的迭代器。

int

lastIndexOf(Object o)返回此列表中指定元素的最后一次出现的索引,如果此列表不包含元素,则返回-1。

ListIterator<E>

listIterator()返回列表中的列表迭代器(按适当的顺序)。

ListIterator<E>

listIterator(int index)从列表中的指定位置开始,返回列表中的元素(按正确顺序)的列表迭代器。

E

remove(int index)删除该列表中指定位置的元素。

boolean

remove(Object o)从列表中删除指定元素的第一个出现(如果存在)。

boolean

removeAll(Collection<?> c)从此列表中删除指定集合中包含的所有元素。

boolean

removeIf(Predicate<? super E> filter)删除满足给定谓词的此集合的所有元素。

protected void

removeRange(int fromIndex, int toIndex)从这个列表中删除所有索引在 fromIndex (含)和 toIndex之间的元素。

void

replaceAll(UnaryOperator<E> operator)将该列表的每个元素替换为将该运算符应用于该元素的结果。

boolean

retainAll(Collection<?> c)仅保留此列表中包含在指定集合中的元素。

E

set(int index, E element)用指定的元素替换此列表中指定位置的元素。

int

size()返回此列表中的元素数。

void

sort(Comparator<? super E> c)使用提供的 Comparator对此列表进行排序以比较元素。

Spliterator<E>

spliterator()在此列表中的元素上创建late-binding故障快速 Spliterator 。**

List<E>

subList(int fromIndex, int toIndex)返回此列表中指定的 fromIndex (包括)和 toIndex之间的独占视图。

Object[]

toArray()以正确的顺序(从第一个到最后一个元素)返回一个包含此列表中所有元素的数组。

<T> T[]

toArray(T[] a)以正确的顺序返回一个包含此列表中所有元素的数组(从第一个到最后一个元素); 返回的数组的运行时类型是指定数组的运行时类型。

void

trimToSize()修改这个 ArrayList实例的容量是列表的当前大小。