prometheuspk
prometheuspk

Reputation: 3815

How can this same result be achieved via a foreach loop in Java

I am trying to make a DeleteRecord() that takes any number of String[][] type arguments. I have made a sort of test function just to see what kind of logice iwould need to apply to make that function. I made it work but I want to use a foreach loop. how can I do that. I have this code:

public void testSomething(String[][]... enteredStrings) {
        for (int i = 0; i < enteredStrings[0].length; i++) {
            for (int j = 0; j < enteredStrings[0][i].length; j++) {
                System.out.println("i -> " + i + " " + "j -> " + j + " " + enteredStrings[0][i][j]);
            }
        }
    }

I know how to make foreach loop in java but I can't do it with a multidimensional array. Thanks in advance.

Upvotes: 0

Views: 135

Answers (2)

jhlu87
jhlu87

Reputation: 4019

For a 3D array with your setup the code below will print the contents of the array. However, I'm not sure how you get the indices with a for each.

public void test2(String[][]... enteredStrings){
    for (String[] iii : enteredStrings[0]){
        for (String jjj: iii){
            System.out.println(jjj);
        }
    }
}

Upvotes: 0

SLaks
SLaks

Reputation: 888293

You need to loop through the String[]s in the outer array of strings-arrays:

for (String[] arr : enteredStrings) {
    for (String str : arr) {
        ...
    }
}

Upvotes: 4

Related Questions