Java:打印出 args 数组中的所有整数

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

Java: Printing out all the integers in args array

javaarraysintegerargs

提问by syncoroll

How do I print out a set of integers from my argsvariable in Java?

如何从argsJava 中的变量打印出一组整数?

I tried:

我试过:

System.out.println("The numbers are " + args.length);

But all that does is print out the number of elements in the array.

但所做的只是打印出数组中元素的数量。

I want it so that if there are 5 arguments passed: 1 2 3 4 5the output should be:

我想要它,以便如果传递1 2 3 4 5了5 个参数:输出应该是:

1, 2, 3, 4, 5

回答by Thilo

Take a look if you like

喜欢的话就看看

  System.out.println("The numbers are "+Arrays.toString(args));

If not, you'll have to loop over the array and format it yourself.

如果没有,您将不得不遍历数组并自己格式化。

回答by Shraddha

But all that does is print out the numbers in the array not the numbers separately.

但所做的只是打印出数组中的数字,而不是单独打印数字。

This seems to be somewhat unclear.

这似乎有些不清楚。

args.lengthgives the length of the array rather than its elements.

args.length给出数组的长度而不是它的元素。

Use a for loop:

使用 for 循环:

System.out.println("The numbers are ");

for(int i=0; i < args.length; i++)
    System.out.print(args[i] + " ");

回答by Marcelo

Try:

尝试:

for(String arg: args)
    System.out.print(arg + " ");