Java args.length 和命令行参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18446509/
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
args.length and command line arguments
提问by Wang Pei The Dancer
I got confused with how to use args.length, I have coded something here:
我对如何使用 args.length 感到困惑,我在这里编写了一些代码:
public static void main(String[] args) {
int [] a = new int[args.length];
for(int i = 0;i<args.length;i++) {
System.out.print(a[i]);
}
}
The printout is 0 no matter what value I put in command line arguments, I think I probably confused args.length with the args[0], could someone explain? Thank you.
无论我在命令行参数中输入什么值,打印输出都是 0,我想我可能将 args.length 与 args[0] 混淆了,有人可以解释一下吗?谢谢你。
回答by Maroun
int
array is initialized to zero (all members will be zero) by default, see 4.12.5. Initial Values of Variables:
int
默认情况下,数组初始化为零(所有成员都为零),参见4.12.5。变量的初始值:
Each class variable, instance variable, or array component is initialized with a default value when it is created ...
For type int, the default value is zero.
每个类变量、实例变量或数组组件在创建时都使用默认值进行初始化......
对于 int 类型,默认值为零。
You're printing the value of the array, hence you're getting 0
.
您正在打印数组的值,因此您将获得0
.
Did you try to do this?
你试过这样做吗?
for (int i = 0; i < args.length; i++) {
System.out.print(args[i]);
}
args
contains the command linearguments that are passed to the program. args.length
is the length of the arguments. For example if you run:
args
包含传递给程序的命令行参数。args.length
是参数的长度。例如,如果您运行:
java myJavaProgram first second
args.length
will be 2 and it'll contain ["first", "second"]
.
args.length
将是 2,它将包含["first", "second"]
.
And the length of the array args is automatically set to 2 (number of your parameters), so there is no need to set the length of args.
并且数组 args 的长度自动设置为 2(您的参数数量),因此无需设置 args 的长度。
回答by Prasad Kharkar
args[0]
is the first element of the args
array.
args.length
is the length of the array
args[0]
是args
数组的第一个元素。
args.length
是数组的长度
回答by Bohemian
I think you're missing a code that converts Strings to ints:
我认为您缺少将字符串转换为整数的代码:
public static void main(String[] args) {
int [] a = new int[args.length];
// Parse int[] from String[]
for (int i = 0; i < args.length; i++){
a[i] = Interger.parseInt(args[i]);
}
for (int i = 0; i < args.length; i++){
System.out.print(a[i]);
}
}
回答by Guillaume
The a
array you iterate on is not the args
that contains the actual arguments. You should try:
a
您迭代的数组不是args
包含实际参数的数组。你应该试试:
public static void main(String[] args) {
for(int i = 0;i<args.length;i++){
System.out.print(args[i]);
}
}
回答by akhil
the arguements you are passing are stored in args array and not your so called array a. by default a properly declared array if not initialized takes the default values of its datatypes. in your case 0.
您传递的参数存储在 args 数组中,而不是您所谓的数组 a 中。默认情况下,如果未初始化,正确声明的数组将采用其数据类型的默认值。在你的情况下 0。
so you do
所以你也是
public static void main(String[] args) {
for(int i = 0;i<args.length;i++){
System.out.print(args[i]);
}
}
or initialize the array a with the args.
或使用参数初始化数组 a。
回答by Renz Mallari
args.length is = 0;
args.length = 0;
if you're looking for this output:
如果您正在寻找此输出:
args[0]:zero
args[1]:one
args[2]:two
args[3]:three
args[0]:0
args[1]:1
args[2]:2
args[3]:3
here is the example...
这是例子...
public static void main(String[] args) {
公共静态无效主(字符串 [] args){
// array with the array name "arg" String[] arg={"zero","one","two","three"};
// 数组名称为“arg” String[] arg={"zero","one","two","three"};
for( int i=0; i<arg.length ;++i)
{
System.out.println("args["+i+"]:"+arg[i]);
}
}
}
you have to give a length to the array ..
你必须给数组一个长度..