Adding multiple BigInteger values to an ArrayList [duplicate]
I want to add multiple BigInteger
values to an ArrayList
. All I have found is exa开发者_JAVA百科mples 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?
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 Comparator
s, 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.
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.
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.
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.
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;
}
}
精彩评论