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
Java: Printing out all the integers in args array
提问by syncoroll
How do I print out a set of integers from my args
variable in Java?
如何从args
Java 中的变量打印出一组整数?
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 5
the 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.length
gives 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 + " ");