Java 将多个 BigInteger 值添加到 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3331794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:16:10  来源:igfitidea点击:

Adding multiple BigInteger values to an ArrayList

javaarraysarraylist

提问by aaa

I want to add multiple BigIntegervalues to an ArrayList. All I have found is examples that repeatedly add single values, each expressed on their own line of code. I'm looking for something like

我想将多个BigInteger值添加到ArrayList. 我发现的只是重复添加单个值的示例,每个值都用自己的代码行表示。我正在寻找类似的东西

ArrayList<BigInteger> array = {bigInt1, bigInt2, bigInt3};

and instead it's:

而是:

ArrayList<BigInteger> array = new ArrayList<BigInteger>();
array.add(bigInt1);
array.add(bigInt2);
array.add(bigInt3);

Can it be done, without adding one element/line or using a for loop?

可以在不添加一个元素/行或使用 for 循环的情况下完成吗?

采纳答案by cletus

I'm not really sure what you're after. You have four alternatives:

我不确定你在追求什么。你有四种选择:

1. Add items individually

1.单独添加项目

Instantiate a concrete Listtype and then call add()for each item:

实例化一个具体List类型,然后调用add()每个项目:

List<BigInteger> list = new ArrayList<BigInteger>();
list.add(new BigInteger("12345"));
list.add(new BigInteger("23456"));

2. Subclass a concrete Listtype (double brace initialization)

2. 子类化一个具体List类型(双括号初始化)

Some might suggest double brace initialization like this:

有些人可能会建议像这样进行双括号初始化:

List<BigInteger> list = new ArrayList<BigInteger>() {{
  add(new BigInteger("12345"));
  add(new BigInteger("23456"));
}};

I recommend not doing this. What you're actually doing here is subclassing ArrayList, which (imho) is not a good idea. That sort of thing can break Comparators, equals()methods and so on.

我建议不要这样做。你在这里实际做的是 subclassing ArrayList,这(恕我直言)不是一个好主意。那种东西可以破坏Comparators、equals()方法等等。

3. Using Arrays.asList()

3. 使用 Arrays.asList()

Another approach:

另一种方法:

List<BigInteger> list = new ArrayList<BigInteger>(Arrays.asList(
  new BigInteger("12345"),
  new BigInteger("23456")
));

or, if you don't need an ArrayList, simply as:

或者,如果您不需要ArrayList,只需如下:

List<BigInteger> list = Arrays.asList(
  new BigInteger("12345"),
  new BigInteger("23456")
);

I prefer one of the above two methods.

我更喜欢上述两种方法之一。

4. Collectionliterals (Java 7+)

4.Collection文字(Java 7+)

Assuming Collection literalsgo ahead in Java 7, you will be able to do this:

假设在 Java 7 中继续使用Collection 字面量,您将能够做到这一点:

List<BigInteger> list = [new BigInteger("12345"), new BigInteger("23456")];

As it currently stands, I don't believe this feature has been confirmed yet.

就目前而言,我认为此功能尚未得到确认。

That's it. Those are your choices. Pick one.

就是这样。这些都是你的选择。选一个。

回答by Chris Dennett

Arrays.asList(new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4"));

You could probably make a method that returns a new BigInteger given a String, called something like bi(..) to reduce the size of this line.

您可能会创建一个方法,该方法在给定字符串的情况下返回一个新的 BigInteger,称为 bi(..) 之类的东西以减少该行的大小。

回答by Jatin

BigIntegerArrays.asList(1, 2, 3, 4);

Where BigIntegerArrays is a custom class which does what you need it to do. This helps if you are doing this often. No rocket science here - ArrayList BigIntegerArrays.asList(Integer... args) will use a FOR loop.

其中 BigIntegerArrays 是一个自定义类,它可以完成您需要做的事情。如果您经常这样做,这会有所帮助。这里没有火箭科学 - ArrayList BigIntegerArrays.asList(Integer... args) 将使用 FOR 循环。

回答by Pascal Thivent

If using a third party library is an option, then I suggest using Lists.newArrayList(E... elements)from Google's Guava:

如果使用第三方库是一种选择,那么我建议使用Lists.newArrayList(E... elements)Google 的Guava

List<BigInteger> of = Lists.newArrayList(bigInt1, bigInt2, bigInt3);

And if mutability isn't required, then use an overload of ImmutableList.of():

如果不需要可变性,则使用以下重载ImmutableList.of()

final List<BigInteger> of = ImmutableList.of(bigInt1, bigInt2, bigInt3);

This is IMO a very elegant solution.

这是 IMO 一个非常优雅的解决方案。

回答by Boann

This is easily accomplished with a helper function or two:

这可以通过一两个辅助函数轻松完成:

import java.util.*;
import java.math.BigInteger;

class Example {
    public static void main(String[] args) {
        ArrayList<BigInteger> array = newBigIntList(
            1, 2, 3, 4, 5,
            0xF,
            "1039842034890394",
            6L,
            new BigInteger("ffff", 16)
        );

        // [1, 2, 3, 4, 5, 15, 1039842034890394, 6, 65535]
        System.out.println(array);
    }

    public static void fillBigIntList(List<BigInteger> list, Object... numbers) {
        for (Object n : numbers) {
            if (n instanceof BigInteger) list.add((BigInteger)n);
            else if (n instanceof String) list.add(new BigInteger((String)n));
            else if (n instanceof Long || n instanceof Integer)
                list.add(BigInteger.valueOf(((Number)n).longValue()));
            else throw new IllegalArgumentException();
        }
    }

    public static ArrayList<BigInteger> newBigIntList(Object... numbers) {
        ArrayList<BigInteger> list = new ArrayList<>(numbers.length);
        fillBigIntList(list, numbers);
        return list;
    }
}