Java: Printing out all the integers in args array
How do I print out a set of integers from my args
variable in 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开发者_Go百科 4 5
the output should be:
1, 2, 3, 4, 5
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.
Try:
for(String arg: args)
System.out.print(arg + " ");
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.
Use a for loop:
System.out.println("The numbers are ");
for(int i=0; i < args.length; i++)
System.out.print(args[i] + " ");
精彩评论