0

Java Enhanced for loop

I applied enhanced for loop on a 2d array. I am getting unexpected output. Please check the code public class pr { public static void main(String[] args) { int listoflist[][]= {{45,34,23},{43,2}}; for (int[] j:listoflist) { // System.out.println(j[0]); for (int k:j) { System.out.print(' '+ k); } System.out.println(); } } }

10th Nov 2019, 6:08 AM
Prabakaran
Prabakaran - avatar
2 Antworten
+ 1
Use double quote for string. In your code you used single quotes which represents a char. A space (' ' character) has a value 32, and this value is added to each of your array elements on each inner loop iteration. System.out.print(" " + k); // double quotes rather than single quotes
10th Nov 2019, 6:20 AM
Ipang
+ 1
@Ipang Thanks for that
10th Nov 2019, 6:24 AM
Prabakaran
Prabakaran - avatar