Reading Data From a 2d Array in Java

In this tutorial we will learn near Two Dimensional Arrays in Coffee programming language.

We utilise two dimensional, 2nd array to store information in rows and columns format.

Declaring a 2D array

Following is the syntax to declare a 2D assortment.

          dataType[][] arrayName;                  

Where, name of the assortment variable is arrayName and its data type is dataType.

We can also declare 2D array in the following mode.

          dataType arrayName[][];                  

This is similar to C programming language.

In the following example nosotros are declaring a 2d array arr of data type int to shop integer values.

          int[][] arr;                  

Now, lets go ahead and create a 2D array.

Creating a 2D array

To create a second array nosotros use the new keyword as shown below.

          arrayName = new dataType[row_size][col_size];                  

Where, arrayName is the name of the array variable of information blazon dataType and it has row_size number of rows and col_size number of columns.

In the following instance we are creating an array arr having 2 rows and three columns and of data blazon int.

          arr = new int[2][3];                  

Proclamation and Creation of 2D array

In the post-obit example we are declaring and creating a second array arr of having 2 rows and three columns and of information blazon int.

          int[][] arr = new int[ii][iii];                  

Assigning and Accessing value of a 2nd array

In the following case nosotros are creating a second array arr having 2 rows and iii columns and of data type int.

To access the element of the assortment we are using the row-index and column-index like arr[r][c] to admission element at rth row and cth column.

          grade Array2DExample {   public static void main(String args[]) {     // create an array     int[][] arr = new int[2][3];      // int variable     int count = ane;          // use for loop to assign value to an array     for (int r = 0; r < 2; r++) {       for (int c = 0; c < 3; c++) {         arr[r][c] = count;         count++;       }     }          // print the value of the array     for (int r = 0; r < 2; r++) {       for (int c = 0; c < 3; c++) {         System.out.print("arr[" + r + "][" + c + "] = " + arr[r][c] + " ");       }       System.out.println();     }   } }                  

Output:

          arr[0][0] = one arr[0][1] = ii arr[0][two] = three  arr[1][0] = four arr[1][1] = 5 arr[1][ii] = half dozen                  

Another way of assigning the values is by using {} curly brackets and using comman separated values.

          class Array2DExample {   public static void main(String args[]) {     // create an array     int[][] arr = {       {1, 2, iii},       {4, five, 6}     };          // print the value of the array     for (int r = 0; r < 2; r++) {       for (int c = 0; c < three; c++) {         Organization.out.impress("arr[" + r + "][" + c + "] = " + arr[r][c] + " ");       }       Organisation.out.println();     }   } }                  

Nosotros can even assign value alphabetize-wise like the following.

          class Array2DExample {   public static void primary(Cord args[]) {     // create an assortment     int[][] arr = new int[ii][3];          // assign value to 1st row having r-index 0     arr[0][0] = ane;     arr[0][one] = two;     arr[0][2] = 3;          // assign value to 2nd row having r-index i     arr[1][0] = 4;     arr[i][1] = v;     arr[one][2] = 6;          // impress the value of the array     for (int r = 0; r < two; r++) {       for (int c = 0; c < 3; c++) {         Organisation.out.print("arr[" + r + "][" + c + "] = " + arr[r][c] + " ");       }       System.out.println();     }   } }                  

Representation of 2d assortment

The following image depicts a 2d array arr.

          // assortment having 2 rows and three columns int[][] arr = {       {one, ii, iii},       {four, 5, half-dozen}     };                  

Example #ane: Write a Java plan to discover the average score of 2 students in three papers

Given, score of first student is 60, 55 and 70 while score of the second educatee is 80, threescore and 41.

We can shop the score of the 2 students in a 2D array having 2 rows and iii columns. The rows volition represent the student and the columns will hold the score of the students.

          class Array2DExample {   public static void main(String args[]) {     // create an array     int[][] score = {       {60, 55, lxx},       {80, sixty, 41}     };      // sum array     int[] sum = new int[2];      // sum of 1st student     sum[0] = 0;      // sum of second student     sum[1] = 0;      // average array     bladder[] avg = new float[2];          // compute sum     for (int r = 0; r < two; r++) {       for (int c = 0; c < three; c++) {         sum[r] += score[r][c];       }     }          // compute average of 1st student     avg[0] = (float)sum[0] / 3;          // compute average of 2nd student     avg[one] = (float)sum[1] / 3;      // print effect     System.out.println("Average score of 1st student = " + avg[0]);     Organisation.out.println("Average score of 2nd educatee = " + avg[1]);   } }                  

Output:

          Average score of 1st pupil = 61.666668 Average score of 2nd student = sixty.333332                  

mitchellsaimed.blogspot.com

Source: https://dyclassroom.com/java/java-two-dimensional-arrays

0 Response to "Reading Data From a 2d Array in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel