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(); } } }
2 Respuestas
+ 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
+ 1
@Ipang Thanks for that