为什么List.add(E)返回boolean而List.Add(int,E)返回void? [英] Why does List.add(E) return boolean while List.Add(int, E) returns void?

查看:1446
本文介绍了为什么List.add(E)返回boolean而List.Add(int,E)返回void?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 javadoc I看到一个ArrayList有一个重载的add方法:

Looking at the javadoc I saw the that an ArrayList has an overloaded add method:


public boolean add(E e)

public boolean add(E e)

将指定的元素附加到此列表的末尾。

Appends the specified element to the end of this list.


public void add(int index,
E element)

public void add(int index, E element)

在指定的位置插入指定的元素这个列表。将当前在该位置的元素(如果有)和任何后续元素(向其索引添加一个元素)移动。

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

注意到第一个返回了 boolean ,而第二个是 void 。事实证明,第一个添加会返回 boolean ,因为:

I noticed that the first one returned a boolean while the second one was a void. As it turns out, the first add HAS to return a boolean because:


返回:
true(由Collection.add(E)指定)

Returns: true (as specified by Collection.add(E))


$ b b

所以我去了 Collection.add( E)



boolean add(E e)


确保此集合包含指定的元素(可选操作)。如果此集合作为调用的结果而更改,则返回true。 (如果此集合不允许重复且已包含指定的元素,则返回false。)

Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

所以我的问题是,为什么 add 指定为返回布尔而不是一个void?当我添加的东西,我期望只做一个操作。

So my question is, why is add specified to return boolean instead of being a void? When I add something I would expect to only do an operation.

我知道还有其他数据结构,而不是ArrayList,不允许重复(如集合)。但即使如此,问题不能按照以下方式解决:

I understand that there's other data structures that, as opposed to ArrayList, do not allow duplicates (such as sets). But even then, couldn't the problem be solved along the lines of:

public void add(E e){
    if(e is not in set){
        add e;
    }
}

/ code> IS在集合中不采取任何操作。为什么最好返回 boolean 而不是 void 方法?

That way if e IS in the set no action is taken. Why is it better to return a boolean instead of the void approach?

推荐答案

Collection.add 是一个非常通用的方法广泛适用)。

Collection.add is a pretty generic method (not in the sense of Java generics -- in the sense of being widely applicable). As such, they wanted a return value that would apply generally.

一些类(如 ArrayList )总是接受元素,因此将始终返回 true 。你是对的,在这些情况下, void 的返回类型也会一样好。

Some classes (like ArrayList) always accept elements, and so will always return true. You're right that in these cases, a return type of void would be just as good.

,如 Set ,有时不允许添加元素。在设置的情况下,如果等于元素已经存在,则会发生这种情况。知道这通常有帮助。另一个例子是有界集合(只能包含一定数量的元素)。

But others, like Set, will sometimes not allow an element to be added. In Set's case, this happens if an equal element was already present. It's often helpful to know that. Another example is a bounded collection (that can only hold a certain number of elements).

你可以问,不能代码只是手动检查?例如,有一组:

You could ask, "can't code just check this manually?" For instance, with a set:

if (!set.contains(item)) {
    set.add(item);
    itemWasAdded(item);
}

这比现在可以做的更冗长, :

This is more verbose than what you can do now, but not a whole lot:

if (set.add(item)) {
    itemWasAdded(item);
}

但这个check-then-act行为不是线程安全的,在多线程应用程序中至关重要。例如,可能另一个线程在您检查 set.contains(item) set.add(item)在第一个代码片段。在多线程场景中,这两个动作真的需要是一个单独的原子操作;从该方法返回 boolean 可以。

But this check-then-act behavior isn't thread safe, which can be crucial in multithreaded applications. For instance, it could be that another thread added an equal item between you checking set.contains(item) and the set.add(item) in the first code snippet. In a multithreaded scenario, those two actions really need to be a single, atomic action; returning boolean from the method makes that possible.

这篇关于为什么List.add(E)返回boolean而List.Add(int,E)返回void?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆