1

I have to loop through a 2D array, create and store a random question, and test the user's response. However, I can't figure out how to properly reference the elements. I'm use to the old syntax of (counter; counter < x; counter++).

How do I reference a specific array element with this syntax? It's quite confusing to me. I need to reference the 5th element in the row to see what the user entered to break from the loop and also to loop through and transpose a 1D array into the 2D array's current row.

    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
}
5
  • 1
    You do not need arrCol = arr2[arrCol]; The code for(int arrCol : arrRow) already assigns the value to arrCol Commented Oct 11, 2018 at 1:32
  • What's arr2 supposed to contain? And what values do arrCol hold that made it the index of arr2? What is clear is arrCol = arr2[arrCol]; is definitely wrong as you are assigning a primitive variable (referenced by value) with new value, which wouldn't change anything. Commented Oct 11, 2018 at 1:36
  • 1
    Consider your 2D Array like a Data Table that contains 100 Rows (y) with 5 Columns (x) in each Row. Iterate through your Array with that in mind: for (int y = 0; y < arr.length; y++) { for (int x = 0; x < arr[y].length; x++) { System.out.print(arr[y][x] + "\t"); } System.out.println(""); }. Commented Oct 11, 2018 at 1:47
  • @ScaryWombat - Maybe I'm not interpreting the book correctly. My understanding is that "for(arrRow[] : arr)" loops through the rows of the arr array and could be considered as arr[x], where the x value increments each time the loop runs. "for(int arrCol : arrRow)" would then loop through that row's columns. I would then interpret that arrCol would represent arr[x][y] where the y value increments each time that loop runs. Based on that, I further assume that "arrCol = arr2[arrCol]" would be the same as "arr[x][y] = arr2[y]". Commented Oct 11, 2018 at 4:58
  • @DevilsHnd - Thanks for that tip. I can get it to work that way no problem, but I'm really trying to understand this unfamiliar syntax from my class's assigned text. They only have a few examples in the book and I just can't seem to grasp the concept right now. Commented Oct 11, 2018 at 5:04

2 Answers 2

2

Your arrCol is an int which is a primitive type variable, so this variable is a value copied from arrRow. If you assign any value to arrCol, it will not be reflected in arrRow.

You should do this instead:

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

I'm not sure what arr2 contains, so I can't be sure if you will encounter ArrayIndexOutOfBoundsException when you read its elements like this.

I would guess that you needed arr2[index] rather than arr2[col].

Sign up to request clarification or add additional context in comments.

1 Comment

arr2 is an int array with 5 elements. the case statement calls a randomized quiz and returns a 5 element array to the arr2 array. IE - if an Addition quiz is called it could contain {1,0,2,3,4}: 1 is the left argument 0 indicates it's an addition question 2 is the right argument 3 is the correct answer 4 is the user's response The second for loop pulls the contents of each of arr2's elements into the current row of the main array. If ever the user keys in a -1, it breaks from the first for loop. It works with a regular for loop, but not this new syntax I'm unfamiliar with.
0

It looks like you cannot use a for-each loop to alter the elements of the array as I was intending to do. I'll have to stick with the typical for loop with a counter.

https://www.geeksforgeeks.org/for-each-loop-in-java/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.