-2

I am attempting to pass as input to a java program via a command line argument an ArrayList of BigIntegers.

I understand calling a program as such:

java myProgram one two

args[0] = "one"
args[1] = "two"

But if I was to run the program as such

java myProgram arrayList 

How would I convert the String[] representation of this Arraylist back to an ArrayList<BigInteger>

The arrayList will be returned from a separate java file that returns an arrayList of BigInetegr, could I use that java program as an input to this one?

I am a little confused as how I would go about this.

Thanks!

weston
  • 51,132
  • 20
  • 132
  • 192
Sean
  • 1,217
  • 8
  • 24
  • 42
  • 1
    Command line arguments are strings. Hence the `String[]`. Just parse them. I suppose you could dump a serialized `ArrayList` in a file and then pipe it to STDIN? Don't see much reason for it however. – Boris the Spider Feb 13 '17 at 18:26
  • You want `"one"` to become `1`? Or are you going to pass actual decimal strings to your program, i.e. `"123"`? – weston Feb 13 '17 at 18:28

1 Answers1

1

As long as you are passing actual decimal strings to your program, e.g. java myProgram 123 456.

List<BigInteger> bigInts = new ArrayList<>(args.length);
for(int i = 0; i < args.length; i++)
    bigInts.add(new BigInteger(args[i]));

Or using Java 8 streams:

List<BigInteger> bigInts = Arrays.stream(args)
                            .map(BigInteger::new)
                            .collect(Collectors.toList());

But if you're passing words to your program, then two things, you will need a custom function to parse English words to BigInteger, plus I doubt anyone will be typing out an integer large enough require a BigInteger.

weston
  • 51,132
  • 20
  • 132
  • 192
  • Might be worth using `Collectors.toCollection(ArrayList::new)` if the OP specifically wants an `ArrayList`. Also, `three billion` is too big to fit into an `int` - words can be quite concise sometimes. In any case, +1 – Boris the Spider Feb 14 '17 at 11:06
  • @BoristheSpider True, but I didn't mention `int` and `three billion` fits in a `long`! – weston Feb 14 '17 at 14:05
  • Meh, the maximum value for a `long` is only nine and quarter quintillion or so. (US "short scale") – Boris the Spider Feb 14 '17 at 14:09
  • @BoristheSpider Sure it's possible to overflow it with not many words: `googleplex`! I still doubt this is a probable use case. As I doubt they need an `ArrayList`. – weston Feb 14 '17 at 14:15
  • @BoristheSpider Just noticed there's [unsigned long](http://stackoverflow.com/a/25248688/360211) in Java 8, so that's 18.5 quintillion. – weston Feb 14 '17 at 14:26
  • I believe that "parseUnsignedLong" is for parsing values not in two's complement into a normal _singed_ `long`. Happy to be proven wrong though - that would certainly be a TIL moment! – Boris the Spider Feb 14 '17 at 14:29
  • @BoristheSpider you're quite correct there, you need to use another function to print back to string correctly: http://ideone.com/YfDDiq `Long` class is still only signed. So you can do addition/subtraction safely with these sudo unsigned values, but [multiplication](http://stackoverflow.com/a/14063665/360211) and division etc won't work as expected. – weston Feb 14 '17 at 14:40