-4

I want to add multiple BigInteger values 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

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?

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
aaa
  • 281
  • 1
  • 2
  • 8
  • This is just the way to do it in Java. There are other language that support constructs that are less verbose. Actually, you don't even have to go that far: [Groovy](http://groovy.codehaus.org/Looping) has them too. – NullUserException Jul 26 '10 at 02:21
  • 5
    why down vote? every one going to sock him . :( in my opinion this is not good. – MrYo Aug 16 '12 at 03:51
  • This is identical to another question that has a three didgit upvote value.....madness. – AncientSwordRage Oct 03 '12 at 08:55

5 Answers5

27

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

1. Add items individually

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

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

2. Subclass a concrete List type (double brace initialization)

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.

3. Using 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:

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

I prefer one of the above two methods.

4. Collection literals (Java 7+)

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

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.

casperOne
  • 73,706
  • 19
  • 184
  • 253
cletus
  • 616,129
  • 168
  • 910
  • 942
  • @Die He added wrapping to make it easier to read; both of those are technically one-liners. The latter is `List list = Arrays.asList(new BigInteger("12345"), new BigInteger("23456"));` – Michael Mrozek Jul 26 '10 at 02:10
  • Wow, those collection literals would be useful so people wouldn't be doing double-brace initialization so much. But it's still not available, not even in JDK-8. – EpicPandaForce Jun 30 '14 at 14:06
6
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.

Jatin
  • 709
  • 4
  • 5
3
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.

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
1

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

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

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

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

This is IMO a very elegant solution.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
0

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;
    }
}
Boann
  • 48,794
  • 16
  • 117
  • 146