1

Looking at a for-each loop but don't know how to do it using regular for loop in Java as in:

for(int i=0; i<length;i++)

Change this for-each loop

    for (int[] bomb: bombs) {

Tried this

`for (int[] bomb = 0; bomb<bombs; bomb++) // doesn't work

Clarification: I know what these two loops mean

for (int[]bomb: bombs)`
for (int i = 0; i<bombs.length; i++){}

If possible, I want their combined functionality of saving the i position in 2D array and saving i as the array itself in one for loop line. In other words, I want the convenience of having the loop position in 2D array and directly grabbing the int[] array in the 2D array.

Context

public class MS {
    public static void main(String[] args) {
//Example of input
        int[][] bombs2 = {{0, 0}, {0, 1}, {1, 2}};
        // mineSweeper(bombs2, 3, 4) should return:
        // [[-1, -1, 2, 1],
        //  [2, 3, -1, 1],
        //  [0, 1, 1, 1]]
    }
    public static int[][] mineSweeper(int[][] bombs, int numRows, int numCols) {
        int[][] field = new int[numRows][numCols];
//////////////////////// Enhanced For Loop ////////////////////
        for (int[] bomb: bombs) {
////////////////////// Change to regular for loop //////////////
            int rowIndex = bomb[0];
            int colIndex = bomb[1];
            field[rowIndex][colIndex] = -1;
            for(int i = rowIndex - 1; i < rowIndex + 2; i++) {
                for (int j = colIndex - 1; j < colIndex + 2; j++) {
                    if (0 <= i && i < numRows &&
                            0 <= j && j < numCols &&
                            field[i][j] != -1) {
                        field[i][j] += 1;
                    }
                }
            }
        }
        return field;
    }
 }
7
  • 2
    There are no fancy loops in java; only loop-by-index and for-each. Commented Jan 7, 2019 at 16:19
  • What do you expect by searching for 'fancy enhanced' loop ? Commented Jan 7, 2019 at 16:21
  • I want a for loop that grabs int[] bomb and iterates over int[][]bombs in the format for(int i = 0...) with i++ so that I know which one it got Commented Jan 7, 2019 at 16:24
  • Possible duplicate of For each loop using 2D array Commented Jan 7, 2019 at 16:24
  • Possible duplicate of Is there a Java equivalent of Python's 'enumerate' function? (based on this comment) Commented Jan 7, 2019 at 16:31

2 Answers 2

3

As per The for Statement following two are the same:

for (int i = 0; i < bombs.length; i++) {
  int[] bomb = bombs[i];
}

or

for (int[] bomb : bombs) {

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

7 Comments

Op is asking the normal way not the enhanced way. "How to loop the normal way in array of array"
Can you iterate with int[] i in the for loop instead of a regular int?
But can you iterate with in[]i with a counter such as for (int[] i =0; i<bombs; i++)
@PL I'm struggling to understand where is the problem. There are two primary ways to loop using for and that's it. If you need additional counter use for (int i ...
@CommonMan both versions are shown, as equivalent, so the OP got an answer here.
|
0

Assuming that I understand your question, just use two, nested for-each style loops; one for the double array and one for each member of the double array. Here is some example code:

public class LearnLoopity
{
  private int[][] doubleListThing = {{0, 0}, {0, 1}, {1, 2}};

  @Test
  public void theTest()
  {
    System.out.print("{");

    for (int[] singleListThing : doubleListThing)
    {
      System.out.print("{");
      for (int individualValue : singleListThing)
      {
        System.out.print(individualValue + " ");
      }

      System.out.print("} ");
    }

    System.out.print("} ");
  }
}

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.