Its a Java programming Declare a twodimensional array of int

It\'s a Java programming.

Declare a two-dimensional array of ints in main. Write a static method to read in the size of each dimension, allocate memory for the array and return it. Write another static method to initialize each element to the row index*10 + column index (for example, the element at [3][4] would have the value 34). Call those methods in main.

Solution

ArrayCreate.java

import java.util.Scanner;


public class ArrayCreate {

  
   public static void main(String[] args) {
       int a[][];
       a = getArray();
       initilizeValues(a);
       System.out.println(\"Array elements are : \");
       for(int i=0; i<a.length; i++){
           for(int j=0;j<a[i].length; j++){
               System.out.print(a[i][j]+\" \");
           }
           System.out.println();
       }
   }
   public static int[][] getArray(){
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter the size of rows: \");
       int rows = scan.nextInt();
       System.out.println(\"Enter the size of columns: \");
       int cols = scan.nextInt();
       int arr[][]= new int[rows][cols];
       return arr;
   }
   public static void initilizeValues(int a[][]){
       for(int i=0; i<a.length; i++){
           for(int j=0; j<a[i].length; j++){
               a[i][j] = i * 10 + j; //i indicates row inde and j iindicates column index
           }
       }
   }

}

Output:

Enter the size of rows:
4
Enter the size of columns:
4
Array elements are :
0 1 2 3
10 11 12 13
20 21 22 23
30 31 32 33

It\'s a Java programming. Declare a two-dimensional array of ints in main. Write a static method to read in the size of each dimension, allocate memory for the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site