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;
    }
 }
P L
  • 29
  • 2
  • 2
    There are no fancy loops in java; only loop-by-index and for-each. – DwB Jan 07 '19 at 16:19
  • What do you expect by searching for 'fancy enhanced' loop ? – Zorglube Jan 07 '19 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 – P L Jan 07 '19 at 16:24
  • Possible duplicate of [For each loop using 2D array](https://stackoverflow.com/questions/13383692/for-each-loop-using-2d-array) – nabster Jan 07 '19 at 16:24
  • Possible duplicate of [Is there a Java equivalent of Python's 'enumerate' function?](https://stackoverflow.com/questions/7167253/is-there-a-java-equivalent-of-pythons-enumerate-function) (based on [this comment](https://stackoverflow.com/questions/54077885/how-to-loop-2d-array-using-for-loop#comment94989654_54077915)) – tobias_k Jan 07 '19 at 16:31
  • But looking at your "context" code: Why do you want that? What do you need the index of the bomb itself for? You only need the indices stored within the bomb, and for that, your current code seems to be as good as it gets. – tobias_k Jan 07 '19 at 16:46
  • I want it to better understand for loops. Don't need it for functionality. – P L Jan 07 '19 at 16:48

2 Answers2

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) {

}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Op is asking the normal way not the enhanced way. "How to loop the normal way in array of array" – Vishwa Ratna Jan 07 '19 at 16:15
  • Can you iterate with int[] i in the for loop instead of a regular int? – P L Jan 07 '19 at 16:17
  • But can you iterate with in[]i with a counter such as for (int[] i =0; i – P L Jan 07 '19 at 16:19
  • @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 ...` – Karol Dowbecki Jan 07 '19 at 16:21
  • @CommonMan both versions are shown, as equivalent, so the OP got an answer here. – Joop Eggen Jan 07 '19 at 16:22
  • Can I do the for loop in one line so that it iterates over stack and it saves the position. in other words the functionality of for (int[]bomb: bombs) with the functionality of the i position in for (int i = 0; i – P L Jan 07 '19 at 16:26
  • @PL From your comment it sounds like you are actually looking for [something like Python's `enumerate` for Java](https://stackoverflow.com/questions/7167253/is-there-a-java-equivalent-of-pythons-enumerate-function)? – tobias_k Jan 07 '19 at 16:30
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("} ");
  }
}
DwB
  • 37,124
  • 11
  • 56
  • 82