up vote -9 down vote favorite

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?

flag
8  
Learn to be more respectful. – NullUserException Jul 26 at 1:58
4  
I gave you an answer and a link to the documentation. If you can't follow the link and read it, programming is not for you. – NullUserException Jul 26 at 2:12
1  
@pax Fortunately, it's rare for jerks to be able to write decent questions – Michael Mrozek Jul 26 at 2:19
5  
@Die It's possible to ask a question without being incredibly rude. I know because I just edited your question to do so – Michael Mrozek Jul 26 at 2:20
1  
@aaa Hey welcome to StackOverflow stop poking people whom you ask for help with a sharp stick, okay? Take a week to think about it, no hurry. – Will Jul 26 at 11:41
show 5 more comments

4 Answers

up vote 10 down vote accepted

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.

link|flag
@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 Jul 26 at 2:10
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 Jul 26 at 2:21
3  
@DieLaughing I guess some people do need a manual in order to talk/type with some civility, heh? – NullUserException Jul 26 at 2:26
3  
@DieLaughing, check out the FAQ 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 Jul 26 at 2:28
up vote 3 down vote
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.

link|flag
up vote 2 down vote
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.

link|flag
up vote 1 down vote

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.

link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.