Java中数组中的命令行参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18419845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:29:11  来源:igfitidea点击:

command line arguments in array in Java

javaarrays

提问by user2711992

I am a very beginner in Java, I have a question regarding to use command line arguments line in array, for example, I tried to type:

我是 Java 的初学者,我有一个关于在数组中使用命令行参数行的问题,例如,我尝试输入:

double []a=Double.parseDouble(args[0]);

however, it said" cannot convert double to double",I cannot figure out it, as I can do

但是,它说“无法将双倍转换为双倍”,我无法弄清楚,因为我可以做到

double a=Double.parseDouble(args[0]);

so what is wrong with using CL arguments input in array then?Thanks

那么在数组中使用 CL 参数输入有什么问题呢?谢谢

采纳答案by arshajii

Simply, Double.parseDouble()returns a doubleas opposed to a double[], so you can't assign it to a variable of type double[].

简单地说,Double.parseDouble()返回 adouble而不是 a double[],因此您不能将其分配给类型为 的变量double[]

If you want to convert all of the strings in argsto doubles, you could try something like

如果要将所有字符串转换argsdoubles,可以尝试类似

double[] a = new double[args.length];
for (int i = 0; i < args.length; i++) {
    a[i] = Double.parseDouble(args[i]);
}

Of course, if you just want args[0], then store the parsed doubleas you are doing in your second snippet; not much sense in using an array in this case.

当然,如果您只是想要args[0],则将解析后的内容存储double在您的第二个代码段中;在这种情况下使用数组没有多大意义。

回答by xagyg

It must be an array that is assigned...

它必须是一个分配的数组...

e.g.

例如

double[] a = new double[] { Double.parseDouble(args[0]) };

回答by Stephen C

however, it said" cannot convert double to double",I cannot figure out it, as I can do

但是,它说“无法将双倍转换为双倍”,我无法弄清楚,因为我可以做到

What it actuallysaid was "cannot convert doubleto double[]". The []is crucial to the meaning of the error message ... and you shouldn't ignore it.

实际上说的是“无法转换doubledouble[]”。的[]是错误信息的意义至关重要......你不应该忽略它。

It is telling you that you cannot treat a doubleas an array of double.

它告诉您不能将 adouble视为double.



If you want to initialize a double[], you need to allocate a doublearray first; e.g.

如果要初始化 a double[],需要先分配一个double数组;例如

double [] a = new double[1];
a[0] = Double.parseDouble(args[0]);

Or you could do that in one statement:

或者你可以在一个声明中做到这一点:

double [] a = new double[]{Double.parseDouble(args[0])};

or even

甚至

double [] a = {Double.parseDouble(args[0])};