How to print the results of an array in one line?

I would like to know if anyone can help me understand why my array result is not on the same line. The results of the code below are printed as follows:

[ 1 2 3 4 5 6 7 8 9 10 ] 

Instead of [1 2 3 4 5 6 7 8 9 10] .

Any thoughts on what I am doing wrong for the results are not included in the system?

 class RangeClass { int[] makeRange(int lower, int upper) { int arr[] = new int[ (upper - lower) + 1 ]; for(int i = 0; i < arr.length; i++) { arr[i] = lower++; } return arr; } public static void main(String arguments[]) { int theArray[]; RangeClass theRange = new RangeClass(); theArray = theRange.makeRange(1, 10); System.out.println("The array: [ "); for(int i = 0; i< theArray.length; i++) { System.out.println(" " + theArray[i] + " "); } System.out.println("]"); } } 
+7
source share
9 answers

You can use a shorter version:

 int theArray[] = {1, 2, 3}; System.out.println(java.util.Arrays.toString(theArray)); 
+26
source

Replace System.out.println with System.out.print as follows:

 System.out.print("The array: [ "); for(int i = 0; i< theArray.length; i++) { System.out.print(" " + theArray[i] + " "); } System.out.println("]"); 

println add a line separator at the end of what you just printed.

+5
source

Use System.out.print instead of System.out.println

+3
source

You can do it on one line using Java 8

Suppose you have this list of integers

 List<Integer> lst = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8 ,9); 

You can print all items in one shot this way

 lst.forEach(nb -> System.out.print(nb + " ")); 

Or in this way

 lst.forEach(System.out::print); 

Result

 1 2 3 4 5 6 7 8 9 
+2
source
 for(int i = 0 ; i < theArray.length;i++){ if(i==0) System.out.print("["+theArray[i]); else if(i==theArray.length-1) System.out.print(","+theArray[i]+"]"); else System.out.print(","+theArray[i]); } 

Output: (for example)

[1,2,5,3,7]

+1
source

int [] arr = {3, 4, 1, 7, 8, 5, 4, 11, 33, 21, 17, 15};

System.out.print (Arrays.toString (arr) .replace ("[", "). Replace ("] "," ") .replace (", "," "));

+1
source

System.out.println changes the line after printing the statement. Therefore, the elements of the array are not on the same line. Try using System.out.print instead, and this will do the trick.

0
source

Use System.out.print () in the place of System.out.println () in all code, because if you use System.out.println (), a new line character is printed after our output to the console every time it is called, but if you use System.out.print (), it will print what you pass it as a parameter. So change your code to

 class RangeClass { int[] makeRange(int lower, int upper) { int arr[] = new int[ (upper - lower) + 1 ]; for(int i = 0; i < arr.length; i++) { arr[i] = lower++; } return arr; } public static void main(String arguments[]) { int theArray[]; RangeClass theRange = new RangeClass(); theArray = theRange.makeRange(1, 10); System.out.print("The array: [ "); for(int i = 0; i< theArray.length; i++) { System.out.print(" " + theArray[i] + " "); } System.out.print("]"); } } 
0
source

Just make a little code change:

 class ArrayDemo { int[] makeRange(int lower, int upper) { int arr[] = new int[ (upper - lower) + 1 ]; for(int i = 0; i < arr.length; i++) { arr[i] = lower++; } return arr; } public static void main(String arguments[]) { int theArray[]; ArrayDemo theRange = new ArrayDemo(); theArray = theRange.makeRange(1, 10); System.out.print("The array: [ "); //Remove println here for(int i = 0; i< theArray.length; i++) { System.out.print(" " + theArray[i] + " "); //Same here } System.out.println("]"); } } 

Output Array: [ 1 2 3 4 5 6 7 8 9 10 ]

0
source

All Articles