views:

188

answers:

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?

+9  A: 

General rudeness aside, 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.

cletus
@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"));`
Michael Mrozek
Look, everyone. I was unfocused and I couldn't shake it. Got frustrated and wow, everyone is REALLY anal about asking questions PROPERLY. Could someone point me to the manual on how to ask StackOverflow questions?
aaa
@DieLaughing I guess some people do need a manual in order to talk/type with some civility, heh?
NullUserException
@DieLaughing, check out the FAQ http://stackoverflow.com/faq under the heading **"Be nice"** - attacking a community as being useless when you're asking a question is a sure-fire way to be ostracised by said community. In any case, you've accepted an answer so maybe we're not so useless after all, eh? :-)
paxdiablo
+2  A: 
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
+4  A: 
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
This is better than my solution :)
Chris Dennett
+1  A: 

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