How To Loop Over 2 Dimensional Array Inwards Java?

Advertisement

Masukkan script iklan 970x90px

How To Loop Over 2 Dimensional Array Inwards Java?

Sabtu, 18 Juli 2020

You tin plough over notice loop over a two-dimensional array inwards Java yesteryear using two for loops, as good known as nested loop. Similarly to loop an n-dimensional array you lot bespeak n loops nested into each other. Though it's non mutual to meet an array of to a greater extent than than three dimension as well as 2D arrays is what you lot volition meet most of the places. It's i of the most useful information construction inwards the programming world. You tin plough over notice purpose a two-dimensional array to brand finite patch machine (FSM) solve patch based problems, you lot tin plough over notice purpose a 2D array to create board games similar Chess, Sudoku as well as Tic-Tac-To as well as you lot tin plough over notice fifty-fifty purpose a two-dimensional array to create 2D arcade games e.g. Tetris, Super Mario Bros as well as hence on. Whatever you lot meet on your shroud is zero but a 2D array which is populated using tiles.

In venture to brand purpose of the 2D array, you lot must know how to populate as well as iterate over it as well as that's what you lot volition larn inwards this article. You tin plough over notice retrieve close 2 dimensional array as a matrix which has rows as well as columns, this deal to visualize contents of an array. In venture to loop over a 2D array, nosotros commencement acquire through each row as well as hence over again nosotros acquire through each column inwards every row. That's why nosotros bespeak 2 loops, nested inwards each other.

Anytime, if you lot desire to come upward out of nested loop, you lot tin plough over notice purpose the suspension statement. If you lot are an absolute beginner as well as but started amongst programming, I recommend reading Head First Programming first. Once you lot went through the book, most of the programming concept e.g. array, string, vector volition brand feel to you.

 array you lot bespeak n loops nested into each other How to loop over 2 dimensional array inwards Java?


Java Program to Loop over 2D Array inwards Java

Here is a Java programme to iterate over a 2 dimensional array inwards Java using traditional for loop. Though it's non necessary to purpose for loop, you lot tin plough over notice fifty-fifty purpose spell loop or advanced for loop inwards Java, it makes feel to start amongst this simplest of programming construct.


In this example, nosotros convey commencement created a 2 dimensional array of size 4x4, which way iv rows as well as iv columns. Then nosotros convey looped over it 2 times, commencement fourth dimension to populate the array amongst integer values as well as the minute fourth dimension to acquire through each index as well as impress their values.

It's non necessary to start looping from commencement chemical percentage e.g. [0, 0] which is commencement row as well as commencement column but if you lot desire to touching on every chemical percentage this is the correct house to start.

Here is the code to loop over 2D array inwards Java :

 for (int row = 0; row < board.length; row++) {     for (int col = 0; col < board[row].length; col++) {        board[row][col] = row * col;     }  }

You tin plough over notice meet that outer loop is going through each row as well as the inner loop is going through each column, this way nosotros acquire over all elements. In the commencement iteration, all elements of the commencement row are processed, but similar iterating over i dimensional array.

Here is the consummate programme :

 array you lot bespeak n loops nested into each other How to loop over 2 dimensional array inwards Java?


Looping over 2D array inwards Java

/**  * Java Program to demonstrate how to loop over two-dimensional array.  * We commencement loop to populate the array as well as afterwards to impress values.   *   * @author WINDOWS 8  */ public class TwoDimensionalArrayDemo{      public static void main(String args[]) {          // let's create board of 4x4         int[][] board = new int[4][4];          // let's loop through array to populate board         for (int row = 0; row < board.length; row++) {             for (int col = 0; col < board[row].length; col++) {                 board[row][col] = row * col;             }         }          // let's loop through array to impress each row as well as column         for (int row = 0; row < board.length; row++) {             for (int col = 0; col < board[row].length; col++) {                 board[row][col] = row * col;                 System.out.print(board[row][col] + "\t");             }             System.out.println();         }     }  }

Output :
0   0   0   0    0   1   2   3    0   2   4   6    0   3   6   9    


BTW, Java doesn't actually convey multi-dimensional arrays, instead, you lot convey arrays of arrays (of arrays ...). So, if you lot desire to loop through the commencement array, you lot inquire for "board[0].length", if you lot desire to loop through the minute array, you lot inquire for "board[1].length".

As I told you, a multi-dimensional array is i of the pop information construction as well as tin plough over notice survive used to stand upward for board games e.g. Chess, Ludo, Sudoku, Tetris as well as as good as useful to describe terrains inwards tile based games. In fact, it is i of the most useful information construction inwards game programming. Another natural purpose of a multi-dimensional array is inwards matrix maths e.g. matrix multiplication, adding 2 matrices, transposing matrices etc.

You tin plough over notice extend this technique to loop through the multi-dimensional array inwards Java. You volition but bespeak as many loops as many dimension your array has. Remember, you lot tin plough over notice declare a two-dimensional array inwards Java without specifying the length of the minute dimension.

Further Learning
Data Structures as well as Algorithms: Deep Dive Using Java
here)
  • 22 Array concept Interview Questions inwards Java (see here)
  • How to impress array values inwards Java? (example)
  • How to compare 2 arrays inwards Java? (example)
  • Data Structures as well as Algorithm Analysis inwards Java (see here)