Back to all blogs

MadAlgos Blog

Basic operations on 2-D arrays ep03

M
Mansha Srivastava14 May 2023
Basic operations on 2-D arrays ep03

Hola programmers!

Here’s a question for you all..

💡 Why did the programmer take up hiking?

Because they heard it was a great way to traverse 2D arrays! It's like walking through nature, but with rows and columns instead of trees and bushes. Plus, you get to enjoy the breathtaking views of data points along the way!

hahaha..😂

 

When it comes to working with 2D arrays in Java, the process of iterating through them may seem daunting at first.

However, fear not! With a little guidance and a sprinkle of humor, you'll soon be navigating those arrays like a pro.

So, buckle up and get ready for a journey through rows and columns, as we uncover the secrets of iterating over 2D arrays in Java. Get ready to discover the treasures hidden within the intricate grids of data, all while enjoying a good chuckle along the way.

 

Now, lets take a look on how to traverse 2D arrays in a technical way.

Traversing a 2D array in Java involves using nested loops to iterate through each element systematically.

 

Let's break it down with a code example:

int[][] matrix = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

int rows = matrix.length;    // Number of rows
int columns = matrix[0].length; // Number of columns

// Traversing the 2D array using nested loops
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < columns; j++) {
        int element = matrix[i][j];
        // Perform operations on each element
        
        System.out.print(element + " "); // Print the element
    }
    System.out.println(); // Move to the next line after each row
}

 

In the above example, we have a 2D array called matrix with 3 rows and 3 columns. We use the length property of the array to determine the number of rows (matrix.length) and columns (matrix[0].length).

Next, we use nested for loops to traverse the array.

The outer loop iterates over the rows, and the inner loop iterates over the columns. We access each element of the array using the indices i and j, respectively.

Inside the nested loops, you can perform any desired operations on each element. In this example, we simply print each element using System.out.print().

Additionally, we print a newline character (System.out.println()) after each row to move to the next line.

 

By traversing the 2D array using nested loops, you can access, manipulate, or perform calculations on each element, enabling you to solve a wide range of problems involving multidimensional data structures efficiently.

Now that you have learned about the basic operations on 2D arrays notice that iterating over 2D arrays in Java is like exploring a maze of numbers, where you navigate through rows and columns like an adventurous mathematician.

 

Homework

Learn about the following:

✅Advance array techniques

  • Using arrays as parameters to methods
  • Returning arrays from methods
  • Creating arrays of objects

 

 

Hope you guys enjoyed reading!

Happy learning.

Just remember, if you ever get lost, follow the wise words of a programmer: "Keep calm and loop on!”

Now what?

Follow us on MADAlgos

 


Check out the following blogs for single-dimension arrays :

Introduction to arrays:

https://madalgos.in/blog-space/18

https://madalgos.in/blog-space/19

Basic operations on arrays:

https://madalgos.in/blog-space/20

https://madalgos.in/blog-space/21

Introduction to multidimensional arrays:

https://madalgos.in/blog-space/22

https://madalgos.in/blog-space/23

https://madalgos.in/blog-space/24