[英]Can you pass an ArrayList as a command line argument for a java program
I am attempting to pass as input to a java program via a command line argument an ArrayList of BigIntegers.我试图通过命令行参数 BigIntegers 的 ArrayList 作为输入传递给 java 程序。
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>
我将如何将此 Arraylist 的
String[]
表示转换回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? arrayList 将从一个单独的 java 文件返回,该文件返回 BigInetegr 的 arrayList,我可以使用那个 java 程序作为这个程序的输入吗?
I am a little confused as how I would go about this.我有点困惑我将如何处理这个问题。
Thanks!谢谢!
As long as you are passing actual decimal strings to your program, eg java myProgram 123 456
.只要您将实际的十进制字符串传递给您的程序,例如
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:或者使用 Java 8 流:
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
.但是,如果您将单词传递给您的程序,那么有两件事,您将需要一个自定义函数来将英语单词解析为
BigInteger
,而且我怀疑有人会输入一个足够大的整数需要BigInteger
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.