Wednesday, 14 May 2025

🛠 How to Fill a 2D Array In Java

 You can use nested loops to assign values:

for (int row = 0; row < matrix.length; row++) {

    for (int col = 0; col < matrix[0].length; col++) {

        matrix[row][col] = row + col;

    }

}


//matrix[0].length gives the number of columns in 2D arrays.

//matrix.length gives the number of rows in 2D arrays.

No comments:

Post a Comment

🚀 Top 3 Java Projects Every Beginner Should Build (With Code Ideas)

 Tired of just printing "Hello World"? It’s time to build something real. 💻 If you're learning Java and want to stand out, ...