Java Multi dimensional tutorial




I would like to present to you, some other approach in java programming using buffered reader to input integers... And loop it according to the size of the array.


import java.io.*;
public class MultiArray {
public static void main(String[] args)throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("This is a Multidimensional Array:");
System.out.println(" Enter the length of rows in array:");
int row = Integer.parseInt(input.readLine());
System.out.println(" Enter the length of column in array:");
int cols = Integer.parseInt(input.readLine());
int MyArray[][]= new int[row][cols];
for(int i=0;i<row;i++) {
for(int j=0;j<cols;j++) {
System.out.println("Enter the elements of array: ");
MyArray[i][j]=Integer.parseInt(input.readLine());
System.out.print("\t"+MyArray[i][j]);
        }
System.out.println("");
     }
displayMethod1(MultiArray);
  }
//End of the main statement here:
public static void displayMethod1(int A[][]) {
for(int rows=0;rows<A.length;rows++) {
for(int cols=0;cols<A[rows].length;cols++) {
System.out.print("\t"+A[rows][cols]);
          }

System.out.println("");
     }
  }
}

Post a Comment

0 Comments