Traversing a 2D Array in Java

One possible solution involves using an algorithm to iterate over each row of the 2D Array and count the number of columns that match “Y”. This value can then be stored as desired. To handle the increasing length of the string, the StringBuilder class can be used for better dynamic string manipulation. Additionally, the whitespace from the string can be removed using a regular expression, without modifying the original string in the array.


Question:

To create and save a random question and then verify the user’s response, I need to iterate through
2D array
. Nevertheless, I am struggling to refer to the elements correctly as I am more familiar with the previous syntax of (counter; counter < x; counter++).

I’m having trouble understanding how to reference a particular array element using this syntax. Specifically, I want to access the 5th element in the array to determine when the user has broken out of a loop and to transpose a 1D array into the current row of
2D Array
.

    for(int arrRow[] : arr)                 //arr is a [100][5] array
    {
        switch(rNum.nextInt(4))             //Creates a random number between 0 and 3 and passes it to a switch statement
        {
            case 0:                         //Generates an Addition question
                arr2 = a.quiz();
                break;
            case 1:                         //Generates a Subtraction question
                arr2 = s.quiz();
                break;
            case 2:                         //Generates a Multiplication question
                arr2 = m.quiz();
                break;
            case 3:                         //Generates a Division question
                arr2 = d.quiz();
        }
        //for (colNum=0; colNum<5;colNum++) //loops through the column in the 2D array and pulls data from returned array
        for(int arrCol : arrRow)
        {
            arrCol = arr2[arrCol];
        }
        if(arrRow[4] == -1)                 //If user enters a -1, breaks from the for loop
        {
            break;
        }
    }
    newTest.printQuestionResult();          //Calls the print function after the user is done or the test is complete
}


Solution 1:

The

arrCol

is a

int

, which is a primitive data type. This means that it stores values that are copied from

arrRow

. Any changes made to

arrCol

will not affect

arrRow

.

You should do this instead:

for (int index = 0; index < arrRow.length; i++)
{
    int col = arrRow[index];
    arrRow[index] = arr2[col];
}

It’s uncertain whether

ArrayIndexOutOfBoundsException

is included in

arr2

, therefore there’s no guarantee that you’ll come across it while reading its contents.

My assumption is that what you required was

arr2[index]

, not

arr2[col]

.


Solution 2:


It appears that modifying the elements of the array using a for-each loop is not possible as per my initial intention. Therefore, I will have to rely on the conventional for loop with a counter to proceed.

Check out this informative article on the “for each” loop in Java available on GeeksforGeeks website.

Frequently Asked Questions