foreach语句如何运用到2维数组
import java.util.Scanner;
public class demo4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("*****请输入行数*****");
int n = input.nextInt() ;
System.out.println("*****请输入列数*****");
int m = input.nextInt() ;
int nums[][]= new int[n][m];
for (int cicle = 0;cicle<2;cicle++){
for ( int i=0 ;i<nums.length;i++){
// for (int j=0 ;j<nums[i].length;j++){ (用这个就没有问题)
for (int j: nums[i] ){ //(问题在这里)
switch (cicle){
case 0:
System.out.println("***请输入第"+(i+1)+"行-第"+(j+1)+"列的数***");
nums[i][j]=input.nextInt();
break;
case 1:
System.out.print(nums[i][j]);
break;
}
}
if (cicle==1)
System.out.println();
}
}
}
}
测试报错为
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at com.yueru.demo4.main(demo4.java:21)
并且j的值始终未变
请问应该如何运用?