I would like to know if anybody could help me to understand why my array result does not come in one single line. The results of the code below is printed as:
[
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 to the results not come in on line?
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("]");
}
}
You can use shorter version:
int theArray[] = {1, 2, 3};
System.out.println(java.util.Arrays.toString(theArray));
Replace System.out.println
by System.out.print
like this:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With