-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?

3
  • 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 has them too. Commented Jul 26, 2010 at 2:21
  • 5
    why down vote? every one going to sock him . :( in my opinion this is not good. Commented Aug 16, 2012 at 3:51
  • This is identical to another question that has a three didgit upvote value.....madness. Commented Oct 3, 2012 at 8:55

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Die He added wrapping to make it easier to read; both of those are technically one-liners. The latter is List<BigInteger> list = Arrays.asList(new BigInteger("12345"), new BigInteger("23456"));
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.
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.

1 Comment

This is better than my solution :)
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.

Comments

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.

Comments

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;
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.