Consider it as an array of arrays and this will work for sure.

int mat[][] = { {10, 20, 30, 40, 50, 60, 70, 80, 90},
                {15, 25, 35, 45},
                {27, 29, 37, 48},
                {32, 33, 39, 50, 51, 89},
              };


    for(int i=0; i<mat.length; i++) {
        for(int j=0; j<mat[i].length; j++) {
            System.out.println("Values at arr["+i+"]["+j+"] is "+mat[i][j]);
        }
    }
Answer from Bharat on Stack Overflow
🌐
Coderanch
coderanch.com › t › 708650 › java › top-populate-array-nested-loop
Is there a way top populate a 2d array using a nested while loop? (Java in General forum at Coderanch)
Note the array initialiser creates a “jagged” array one of whose elements is length zero. That is a legitimate thing to do. Since all loops (except using continue in a for loop) can be implemented with while, let's try that:-
Discussions

Java - How to populate 2d array with nested while loops? - Stack Overflow
I need to populate a double array with user input by using nested while loops only. More on stackoverflow.com
🌐 stackoverflow.com
Homework - Do While loop with 2d array

i and j are local to the for loops, you should declare i and j outside of the for loops at the top if you want to use them with a do while loop. This allows for a greater scope of the program

More on reddit.com
🌐 r/javahelp
3
8
December 5, 2019
java - How to make while loop work properly to correct given 2D array - Stack Overflow
I'm trying to write a method that will take a two-dimensional array as an input, and return a new 2D array in which all the zeroes are removed from the array. Also, if there is an element in the ... More on stackoverflow.com
🌐 stackoverflow.com
Multi-dimensional arrays topic in java
2d arrays concept isn't hard really. Usually, the first loop is iterating over 'objects' inside outer array. Second loop over 'objects' inside inner arrays. In this case, outer loop is actually Y-AXIS, and inner loop X-AXIS (just as a note, because usually people say opposite :D). Example: Iterate over the array and print numbers in increasing order. int[][] arr = {{1, 2, 3, 4}, {5}, {6, 7, 8}, {9, 10}}; So, what do we need to do? As you can see, if we do default iteration over an array it will be sorted without any other manipulations. "I need to iterate over each inner array and each element of an inner array" for (int y = 0; y < arr.length; y++) { for (int x = 0; x < arr[y].length; x++) { System.out.println(arr[y][x]); } } What about this? int[][] arr = { {1, 5, 9}, {2, 6, 10}, {3, 7, 11}, {4, 8, 12} }; Well as you can see, you need to iterate column-wise. Lets see how we can do it. for (int x = 0; x < arr[0].length; x++) { for (int y = 0; y < arr.length; y++) { System.out.println(arr[y][x]); } } Let's actually think about it as X, Y coordinates. We changing Y coordinate, but our X stay the same until the end of the column. Then we change X by 1 (moving to the next column), and do iteration over inner loop again. Note that, the matrix must be rectangular. Good task to do using only one while loop with 2d array: https://hyperskill.org/learn/step/1931 I sumbitted an example with comments, so you can see how to do it and some intuition behind it. Let's see an example of 3d array. int[][][] arr = { {{1, 2, 3, 4}, {}}, {{5, 6, 7, 8, 9}, {10, 11}}, {}, {{12, 13, 14, 15}} }; How to print each element? for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { for (int k = 0; k < arr[i][j].length; k++) { System.out.println(arr[i][j][k]); } } } Nothing new, iterate over each element of the most outer array, then iterate over elements of the inner array, and again iterate over elements of the inner array. ps. Wait, did I just said what is already in the theory? lol Just ask something specific then. More on reddit.com
🌐 r/Hyperskill
7
5
June 23, 2020
🌐
Sololearn
sololearn.com › en › Discuss › 2463038 › how-to-print-2-d-elements-in-array-using-while-loop-java
How to print 2 d elements in array using while loop...(java)
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
Reddit
reddit.com › r/javahelp › homework - do while loop with 2d array
r/javahelp on Reddit: Homework - Do While loop with 2d array
December 5, 2019 -

Hello,

I'm trying to finish up an assignment and am in need of some direction. The problem is to ask a user to enter some data for some students. The user will enter the number of classes left to graduate (1-20) and how many courses each term (1-5).

I currently have the program setup to accept any range of data and it is working no problems, but i'm not sure how I need to go about setting up the do/while loop properly. So far I have:

import java.util.Scanner;

public class Chpt8_Project {

public static void main(String\[\] args) {

	Scanner input = new Scanner([System.in](https://System.in));

	double\[\]\[\] students = new double\[4\]\[2\];

	// Enter number of courses left to graduate(1-20) and how many courses to take each term

	for (int i = 0; i < students.length; i++) {

		System.out.print("Enter classes remaining and taking each term for student " + (i+1) + ": ");

		for (int j = 0; j < students\[i\].length; j++) {

students[i][j] = input.nextDouble();

		}				

	}

I've tried to add a do / while loop with the do part before the 1st for loop but when I attempt to add the while condition it tells me the variables i and j don't exist. Any tips about how to tackle this problem will be greatly apprieciated.

Thanks.

🌐
Blogger
javarevisited.blogspot.com › 2015 › 09 › how-to-loop-two-dimensional-array-in-java.html
How to loop over two dimensional array in Java? Example
You can loop over a two-dimensional array in Java by using two for loops, also known as nested loop. Similarly to loop an n-dimensional array you need n loops nested into each other.
Find elsewhere
🌐
Codecademy
codecademy.com › learn › apcs-arrays-and-loops › modules › apcs-two-dimensional-arrays › cheatsheet
Arrays and Loops: Two-Dimensional Arrays Cheatsheet | Codecademy
When a loop is nested inside another loop, the inner loop must complete all its iterations before the outer loop can continue. ... In Java, 2D arrays are stored as arrays of arrays.
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit8-2DArray › topic-8-2-2D-array-loops-Day2.html
8.2.5. Enhanced For-Each Loop for 2D Arrays (Day 2) — CSAwesome v1
... What will the following code print out? Can you complete the method called getTotalForCol that gets the total for a column? To do this, you must loop through the rows. The array’s length will tell you how many rows you have since it is an array of arrays, while ...
🌐
Quora
quora.com › Can-we-use-while-loops-to-iterate-through-a-multi-dimensional-array
Can we use while loops to iterate through a multi-dimensional array? - Quora
Answer (1 of 4): Yes, you can. You can use nested while loops to iterate through a multi-dimensional array. The number of loop scope is dependent to the number of the array’s dimension. Here is an example of running through a 2D array. The mechanism is the same to other dimensions such as ...
🌐
Devcubicle
devcubicle.com › two-dimensional-array-for-loop-java
How to use for loop with two dimensional array in Java - DevCubicle By Cloud Tech
December 29, 2019 - Most of the problems that include board, matrix or grid can be solved using two dimensional array. So it becomes very important to understand how to assign values to two dimensional array and loop over it. Java doesn’t support multidimensional array by default. When we implement a 2d array, ...
🌐
Coding Rooms
codingrooms.com › blog › iterating-through-2d-array-java
Iterating Through 2D Array Java
October 5, 2020 - When you become comfortable with the for loop, try using “for-each” loops with 2D arrays.
🌐
Codefinity
codefinity.com › courses › v2 › 8204075c-f832-4cb9-88b1-4e24e74ebdcb › 208e0693-9cc3-4d38-b477-9af9eaef76cb › bb303c84-2df8-4a9a-835d-c45bb713ea32
Learn Challenge: Iteration in Two-Dimensional Array | Arrays
for (int i = 0; i < twoDimensionalArray.length; i++): the first for-loop iterates over the rows of our matrix. While we're on the first row, our program enters the nested loop, which doesn't impact the outer loop.
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit8-2DArray › topic-8-2-2D-array-loops-Day1.html
8.2.1. Nested Loops for 2D Arrays (Day 1) — CSAwesome v1
The array is passed in as an argument to the method. ... The number of times this loop executes is the number of rows times the number of columns. ... The following has the correct code to find the largest value in a 2D array. Drag the blocks from the left into the correct order on the right ...
🌐
Reddit
reddit.com › r/hyperskill › multi-dimensional arrays topic in java
r/Hyperskill on Reddit: Multi-dimensional arrays topic in java
June 23, 2020 -

I've been working through the hyperskills.org java track and have gotten hung up on multidimensional arrays. I thought maybe I missed something on a previous topic so I have gone back and reviewed for-loops and arrays, which I think I have a good grip on now. Still when I come back and look at the code practicals for working with md arrays I'm still feeling just as confused as when I first encountered them. Surely I'm over-complicating the matter. Any suggestions on moving forward?

Top answer
1 of 3
5
2d arrays concept isn't hard really. Usually, the first loop is iterating over 'objects' inside outer array. Second loop over 'objects' inside inner arrays. In this case, outer loop is actually Y-AXIS, and inner loop X-AXIS (just as a note, because usually people say opposite :D). Example: Iterate over the array and print numbers in increasing order. int[][] arr = {{1, 2, 3, 4}, {5}, {6, 7, 8}, {9, 10}}; So, what do we need to do? As you can see, if we do default iteration over an array it will be sorted without any other manipulations. "I need to iterate over each inner array and each element of an inner array" for (int y = 0; y < arr.length; y++) { for (int x = 0; x < arr[y].length; x++) { System.out.println(arr[y][x]); } } What about this? int[][] arr = { {1, 5, 9}, {2, 6, 10}, {3, 7, 11}, {4, 8, 12} }; Well as you can see, you need to iterate column-wise. Lets see how we can do it. for (int x = 0; x < arr[0].length; x++) { for (int y = 0; y < arr.length; y++) { System.out.println(arr[y][x]); } } Let's actually think about it as X, Y coordinates. We changing Y coordinate, but our X stay the same until the end of the column. Then we change X by 1 (moving to the next column), and do iteration over inner loop again. Note that, the matrix must be rectangular. Good task to do using only one while loop with 2d array: https://hyperskill.org/learn/step/1931 I sumbitted an example with comments, so you can see how to do it and some intuition behind it. Let's see an example of 3d array. int[][][] arr = { {{1, 2, 3, 4}, {}}, {{5, 6, 7, 8, 9}, {10, 11}}, {}, {{12, 13, 14, 15}} }; How to print each element? for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { for (int k = 0; k < arr[i][j].length; k++) { System.out.println(arr[i][j][k]); } } } Nothing new, iterate over each element of the most outer array, then iterate over elements of the inner array, and again iterate over elements of the inner array. ps. Wait, did I just said what is already in the theory? lol Just ask something specific then.
2 of 3
2
Working with pen and paper and writing everything in detail can help.
🌐
Medium
medium.com › @bhagya7552 › two-dimensional-array-in-java-e76b335f484e
Two Dimensional Array in Java. Hello guys 🤗 , | by Bhagya Ariyadasa | Medium
May 29, 2020 - When you initially declare a two-dimensional array, you must specify the first dimension at least. Otherwise it is not correct. ... Without any further initialization, it starts out holding null. In addition to this, we can also use 2 for loops to assign data automatically. It’s not necessary to use for loop, you can even use while loop or advanced for loop in Java. In here, I have created a 2D array of size 4 rows and 3 columns.
🌐
GeeksforGeeks
geeksforgeeks.org › java › print-2-d-array-matrix-java
Print a 2D Array or Matrix in Java - GeeksforGeeks
1 month ago - To print all elements of a 2D array, we can use nested loops or built-in methods from the Arrays class. ... The simplest way to print a 2D array is by using two for loops. The outer loop traverses the rows, while ...
🌐
Learn
learn.java › learning › tutorials › CS14Lists › iterate-over-2darray
Iterating over a Two-Dimensional Array - Learn.java
An introduction to the Java programming language and the use of variables and classes. Algorithms are composed of three building blocks: sequencing, selection, and iteration. Pull together information from the previous two units to create new, user-defined reference data types. An introduction to data structures: array, ArrayList, and 2D ...