Java args.length 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24339479/
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-14 11:39:23  来源:igfitidea点击:

What's the meaning of args.length?

java

提问by Harshit

recently i've come across this piece of code and i really want to understand when and how to use this args.length

最近我遇到了这段代码,我真的很想了解何时以及如何使用这个 args.length

import java.util.*; 

class Average
{

public static void main(String args[])

{

Scanner sc = new Scanner(System.in);

int choice;

int a=0,min=0,max=0,x;

int n = args.length;

System.out.println("1-Sum");

System.out.println("2-Average");

System.out.println("3-Minimum");

System.out.println("4-Maximum");

System.out.println("enter your choice");

choice = sc.nextInt();

for(int i=0;i<n;i++)
{

a+=Integer.parseInt(args[i]);

}

switch(choice)

{

case 1: System.out.println("The sum is :" +a/n);

break

case 2: System.out.println("The Average is :" +a/n);

break

case 3: for(int i=0;i<n-1;i++)

{

x=Integer.parseInt(args[i]);

if(x<Integer.parseInt(args[i+1]))

min=x;

else min=Integer.parseInt(args[i+1]);

}

System.out.println("The minimum is :" +min);

break;

case 4: for(int i=0;i<n-1;i++)

{

x=Integer.parseInt(args[i]);

if(x>Integer.parseInt(args[i+1]))

max = x;

else max=Integer.parseInt(args[i+1]);

}

System.out.println("The maximum is :" +max);

break;

}

}

}

采纳答案by Mandar Pandit

The main()method is initial point where java program execution starts. That's why the main()method is public, staticand void. The parameters passed to main()method are String args[]i.e a String array. It is not necessarily the variable name can be only args, it can be any variable name i.e String names[].

main()方法是java程序执行开始的起始点。这就是main()方法是public,static和的原因void。传递给main()方法的参数是String args[]一个字符串数组。不一定是变量名只能是args,它可以是任何变量名即String names[]

Now, why args.lengthis zero, when no arguments passed:

现在,当没有传递参数时,为什么args.length为零:

When any java program is run from command line, it is run as java ProgName. The command line arguments are passed to java program as java ProgName Arg1 Arg2. Here in this example, two arguments are passed to java program ProgName. It is simple that, the arguments are passed to java program is the same way we run a command with parameters on any operating system. The arguments are passed along with command just by separator character as "space".

当任何 Java 程序从命令行运行时,它都以java ProgName. 命令行参数作为java ProgName Arg1 Arg2. 在此示例中,将两个参数传递给 java program ProgName。很简单,将参数传递给 java 程序的方式与我们在任何操作系统上运行带参数的命令的方式相同。参数仅通过分隔符作为“空格”与命令一起传递。

Java Interpreter interprets these arguments and pass to main()method of java program. When we pass arguments to java program from command line it is stored in the args[]String Array. As here two arguments are passed, while running java program args[0]and args[1]will be allowed, but args[2]will not. Same way if no arguments are passed, so java will not even allow args[0].

Java 解释器解释这些参数并传递给main()java 程序的方法。当我们从命令行向 java 程序传递参数时,它存储在args[]字符串数组中。由于这里有两个参数传递,运行Java程序时args[0],并args[1]会被允许的,但args[2]不会。如果没有传递参数,同样的方式,所以 java 甚至不允许args[0].

Java interprets the command line arguments as String array, as if we pass 2 arguments it is an array with args[0], args[1], if we pass 4 arguments it is an array with args[0], args[1], args[2], args[3]and when no arguments are passed it is still an String array object with no elements.

Java 将命令行参数解释为 String 数组,就好像我们传递 2 个参数它是一个带有 的数组args[0], args[1],如果我们传递 4 个参数它是一个带有 的数组,args[0], args[1], args[2], args[3]并且当没有传递参数时它仍然是一个没有元素的 String 数组对象。

Thus, even when we do not pass any command line arguments to java program, still the args.lengthis equal to Zero - (0).

因此,即使我们没有将任何命令行参数传递给 java 程序,仍然args.length等于零- (0)

回答by Christian

When you run a program, you can run the program passing command-line arguments. If the main method is:

当你运行一个程序时,你可以运行传递命令行参数的程序。如果主要方法是:

public static void main(String args[])

the arguments will be received in the array of Stringcalled args. Now, regarding your question:

参数将在被String调用的数组中接收args。现在,关于你的问题:

What does args.lengthmean?

什么args.length意思?

It's the value of the built-in property lengthfrom arrays, which will be its fixed size. Note that arrays in Java are a special kind of objects.

它是来自数组的内置属性length的值,这将是它的固定大小。请注意,Java 中的数组是一种特殊的对象。

回答by Luke Peterson

args.length is the number of elements in the args[] array. The args[] array contains the parameters passed to the main function from the command line.

args.length 是 args[] 数组中的元素数。args[] 数组包含从命令行传递给 main 函数的参数。

回答by Seal

It is the number of elements in the argsarray, that was defined in public static main(String[] args).

它是args数组中的元素数,在public static main( String[] args) 中定义。