- 1

How to print an array elements in a single line in java?

18th Aug 2017, 4:29 PM
Spider
4 Respostas
+ 4
By single line I assume you want something that's already been made for you. Then you can use toString in the Arrays class: int[] ex = {3,2,1}; System.out.println(Arrays.toString(ex)); It's pretty much the same thing Rishci said, but probably without concatenation. (They likely used StringBuilder)
18th Aug 2017, 5:56 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
use System.out.print() instead of System.out.println() //example class ABC { public static void main(String args[]) { int a[]={1,2,3,4,5}; int l=a.length;//finds the length of the array for(int i=0;i<l;i++) System.out.print(a[i]+" "); } }
18th Aug 2017, 5:11 PM
Rishi Anand
Rishi Anand - avatar
+ 1
Add each element to a string using +=.
18th Aug 2017, 4:33 PM
Bagshot
Bagshot - avatar
0
thanks
18th Aug 2017, 5:38 PM
Spider