i have start to learn programming from scratch, now i'm in array concept. i have doubt in multidimensional array,the doubt is i'm getting the user input as matrix range and value and trying to traverse the array but i got an runtime error. this is my program:
public class Multi {
/**
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int arr[][] = new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
arr[i][j] = sc.nextInt();
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.println(arr[j][i]);
}
System.out.println("\n");
}
sc.close();
}
}
Input: First line contains 2 space separated integers,
N total rows,
M - total columns.
Each of the next
N lines will contain
M space separated integers.
Output: Print M lines each containing N space separated integers.
(i got this error)..
3 5
13 4 8 14 1
9 6 3 7 21
5 12 17 9 3
Exception in thread "main" 13 9 5
java.lang.ArrayIndexOutOfBoundsException: 3
at hackerearth.Multi.main(Multi.java:24)