1

I was able to use for loop with int multidimensional arrays but I'm unable to reproduce it with multi arrays.

public class array {

public static void main(String[] args) {
    String[][] words = new String[2][3];
    words[0][0] = "a";
    words[0][1] = "b";
    words[0][2] = "c";
    words[1][0] = "d";
    words[1][1] = "e";
    words[1][2] = "f";
   }
}

Would love some help on how to iterate that

For reference, this was what I did for int

int[][] multi = {
        {3, 4, 5},
        {2, 3, 5, 6, 7},
        {112, 3}
    };
    for (int row = 0; row < multi.length; row++) {
        for (int col = 0; col < multi[row].length; col++) {
            System.out.print(multi[row][col] + " ");
4
  • show sample code which fails for you Commented Feb 27, 2017 at 19:26
  • for (String[] word : words) { for (String character : word) {...} } Commented Feb 27, 2017 at 19:26
  • try nested loop. for(int i=0;i<rolSizei++) { for(int j=0;j<colSize;j++) {words[i][j] ...}} Commented Feb 27, 2017 at 19:28
  • 1
    Please edit you question to provide the complete code: somebody who wants to help you should be able to copy your code, paste it in an editor and run it without having to add anything. Commented Feb 27, 2017 at 19:30

3 Answers 3

1

You are almost there, adapt the for loops, dont forget every row is an array as well.....

    String[][] words = new String[2][3];
    words[0][0] = "a";
    words[0][1] = "b";
    words[0][2] = "c";
    words[1][0] = "d";
    words[1][1] = "e";
    words[1][2] = "f";
    for (int row = 0; row < words.length; row++) {
        for (int col = 0; col < words[row].length; col++) {
            System.out.println(words[row][col]);
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This is actually the method I was trying to accomplish, but it seems I inadvertenly tried to mix the other suggestion above (nested foreach loop) with this one. It works perfectly
1

With Java 8, you can do the following iterate and print the 2d:

Stream.of(words).map(Arrays::toString).forEach(System.out::println);

Output:
a
b
c
d
e
f 

Just print as one dimensional arrays using Arrays.toString()

Stream.of(words).map(Arrays::toString).forEach(System.out::println);

Output:

[a, b, c]
[d, e, f]

2 Comments

Not exactly. Instead of map(Arrays::toString) you would want to use flatMap(Stream::of). :-)
Corrected my answer based on ur comment
0

How to use For Loop with (Multidimensional) Array of Strings?

String[][] words = new String[2][3];
    words[0][0] = "a";
    words[0][1] = "b";
    words[0][2] = "c";
    words[1][0] = "d";`
    words[1][1] = "e";
    words[1][2] = "f";

Using nested for each loop

One way to accomplish such task is to use nested for each loop, however having said there are other solutions to accomplish the same task.

for(String[] word : words)){
 for(String currentWord : word)System.out.println(currentWord); // this is just explanatory, which you can change with what ever you wish to accomplish with this loop.
}

Another way:

Using nested for loop

for(int i = 0 ;i < 2; i++) {
 for(int j = 0 ;j < 3; j++) {
    System.out.println(words[i][j]); // this is just explanatory, which you can change with what ever you wish to accomplish with this loop.
 }
}

2 Comments

Thank you. The nested foreach loop above works just fine, and is actually preferable now that I know it. I did attempt something like this, but I only declared the first part 'for(String[]word:words){' and then jumped to print. I tried the nested for loop which is the method I was working on but I couldn't get it to work with the code you suggested. I'm not sure if this is because of rowSize & columnSize because changing and using the suggestion below worked.
@JeffOgah the nested for loop does work. I will update the answer to show you exactly what you need.

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.