Java列表add()和addAll()方法

本文详细介绍了Java中List接口的add()和addAll()方法的使用,包括如何将元素添加到列表的末尾或指定位置,以及如何添加集合中的元素。同时,还讨论了在某些情况下,如使用Arrays.asList()和Collections.unmodifiableList()时,这些方法可能会抛出UnsupportedOperationException异常。

1. Java清单add() (1. Java List add())

This method is used to add elements to the list. There are two methods to add elements to the list.

此方法用于将元素添加到列表中。 有两种方法可以将元素添加到列表中。

  1. add(E e): appends the element at the end of the list. Since List supports Generics, the type of elements that can be added is determined when the list is created.

    add(E e) :将元素追加到列表的末尾。 由于列表支持泛型,因此可以在创建列表时确定可以添加的元素类型。
  2. add(int index, E element): inserts the element at the given index. The elements from the given index is shifted towards right of the list. The method throws IndexOutOfBoundsException if the given index is out of range.

    add(int index,E element) :在给定索引处插入元素。 给定索引中的元素将移至列表的右侧。 如果给定索引超出范围,则该方法将抛出IndexOutOfBoundsException

Let’s look at some examples of List add() methods.

让我们看一下List add()方法的一些示例。

package com.journaldev.examples;

import java.util.ArrayList;
import java.util.List;

public class ListAddExamples {

	public static void main(String[] args) {

		List<String> vowels = new ArrayList<>();

		vowels.add("A"); // [A]
		vowels.add("E"); // [A, E]
		vowels.add("U"); // [A, E, U]

		System.out.println(vowels); // [A, E, U]

		vowels.add(2, "I"); // [A, E, I, U]
		vowels.add(3, "O"); // [A, E, I, O, U]

		System.out.println(vowels); // [A, E, I, O, U]
	}
}
Recommended Readings: 推荐读物

2. Java列表addAll() (2. Java List addAll())

This method is used to add the elements from a collection to the list. There are two overloaded addAll() methods.

此方法用于将集合中的元素添加到列表中。 有两个重载的addAll()方法。

  1. addAll(Collection<? extends E> c): This method appends all the elements from the given collection to the end of the list. The order of insertion depends on the order in which the collection iterator returns them.

    addAll(Collection <?extends E> c) :此方法将给定集合中的所有元素追加到列表的末尾。 插入的顺序取决于集合迭代器返回它们的顺序。
  2. addAll(int index, Collection<? extends E> c): we can use this method to insert elements from a collection at the given index. All the elements in the list are shifted towards the right to make space for the elements from the collection.

    addAll(int index,Collection <?extends E> c) :我们可以使用此方法从给定索引处的集合中插入元素。 列表中的所有元素都向右移动,以为集合中的元素腾出空间。

Let’s look at a simple example of List addAll() methods.

让我们看一下List addAll()方法的一个简单示例。

package com.journaldev.examples;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ListAddAllExamples {

	public static void main(String[] args) {

		List<Integer> primeNumbers = new ArrayList<>();

		primeNumbers.addAll(Arrays.asList(2, 7, 11));

		System.out.println(primeNumbers); // [2, 7, 11]

		primeNumbers.addAll(1, Arrays.asList(3, 5));

		System.out.println(primeNumbers); // [2, 3, 5, 7, 11]
	}
}

3.带有列表add()方法的UnsupportedOperationException (3. UnsupportedOperationException with List add() Methods)

If you look at the List add() and addAll() method documentation, the operation is mentioned as optional.

如果您查看List的add()和addAll()方法文档,则该操作被视为可选操作。

It means that the list implementation may not support it. In this case, the list add() methods throw UnsupportedOperationException.

这意味着列表实现可能不支持它。 在这种情况下,列表add()方法将引发UnsupportedOperationException。

There are two common scenarios where you will find this exception when adding elements to the list.

在将元素添加到列表中时,有两种常见的情况,在此情况下您会发现此异常。

  1. Arrays.asList(): This method returns a fixed-size list because it’s backed by the specified array. Any operation where the list size is changed throws UnsupportedOperationException.

    Arrays.asList() :此方法返回固定大小的列表,因为它由指定的数组支持。 更改列表大小的任何操作都将引发UnsupportedOperationException。
  2. Collections.unmodifiableList(List l): This method returns a unmodifiable view of the given list. So the add() operations throw UnsupportedOperationException.

    Collections.unmodifiableList(List l) :此方法返回给定列表的不可修改视图。 因此,add()操作将引发UnsupportedOperationException。

Let’s look at a simple example of UnsupportedOperationException with add operation of both these types of lists.

让我们看一下UnsupportedOperationException的简单示例,其中包含这两种类型的列表的添加操作。

jshell> List<Integer> ints = Arrays.asList(1,2,3);
ints ==> [1, 2, 3]

jshell> ints.add(4);
|  Exception java.lang.UnsupportedOperationException
|        at AbstractList.add (AbstractList.java:153)
|        at AbstractList.add (AbstractList.java:111)
|        at (#4:1)

jshell> List<String> strs = new ArrayList<>();
strs ==> []

jshell> strs.add("A");
$6 ==> true

jshell> List<String> strs1 = Collections.unmodifiableList(strs);
strs1 ==> [A]

jshell> strs1.add("B");
|  Exception java.lang.UnsupportedOperationException
|        at Collections$UnmodifiableCollection.add (Collections.java:1058)
|        at (#8:1)
Java List Add UnsupportedOperationException

Java List add() UnsupportedOperationException

Java列表add()UnsupportedOperationException

参考资料 (References)

翻译自: https://www.journaldev.com/33297/java-list-add-addall-methods

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值