Private Xoxo
Private Xoxo

Reputation: 11

when filling a 2d array it gets null values

Im trying to initialise all the elements of the 2d array into a string "EMPTY". but When ever I try to initialise the array it gets null values. I checked errors in the for loop but couldn't see any

public static void arr_2d(){
    String [][] arr = new String[3][2];
    for (int i = 0; i < arr.length; i++) {
        for (int a = 0; a < arr[i].length; a++) {
            arr[i][a] = "EMPTY";
        }


        for (int b = 0; b < arr.length; b++) {
            for (int j = 0; j < arr[b].length; j++) {
                System.out.print(arr[b][j] + " ");
            }
            System.out.println();
        }
    }
}

Upvotes: 0

Views: 455

Answers (4)

Emanuel Couto
Emanuel Couto

Reputation: 441

Although the answers you have are correct I will add that one problem is your code style is prone to errors.

Your mistake was traversing the array incorrectly. The correct way is traversing the array twice, one of filling and another for printing, but instead it seems you have attempted to do everything in one shot. That mistake can be avoided with a better code style.

This is how I would have written your code in imperative style:

String[][] arr = new String[3][2];
for (String[] a : arr)
    Arrays.fill(a, "EMPTY");

for (String[] a : arr)
    System.out.println(Arrays.toString(a));

Notice the code is much shorter, so there's less chances of mistakes. It's also a lot more obvious that you're traversing twice.

Instead of traversing an array explicitly:

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

Use the implicit for loop:

for (String[] value: arr)

Instead of filling an array explicitly:

for (int a = 0; a < arr[i].length; a++) {
    arr[i][a] = "EMPTY";
}

Use the already provided fill method:

Arrays.fill(value, "EMPTY");

Instead of printing an array explicitly:

for (String string : strings) {
    System.out.print(string + " ");
}
System.out.println();

Use the already provided print method:

for (String[] a : arr)
    System.out.println(Arrays.toString(a));

However, I would have written in functional style:

String [][] arr = new String[3][2];
Arrays.stream(arr)
      .forEach(a -> Arrays.fill(a, "EMPTY"));
Arrays.stream(arr)
      .map(Arrays::toString)
      .forEach(System.out::println);

One particular advantage is that you are encouraged to think in a more abstract way. Instead of thinking how to explicitly set or print each element of the array, you are encouraged to use methods that implicitly traverse, transform or perform generic computations on all elements of the array.

Upvotes: 0

Akash R
Akash R

Reputation: 129

Check the comments given below in the snippet:

    public static void arr_2d(){
        String [][] arr = new String[3][2];
        for (int i = 0; i < arr.length; i++) {
            for (int a = 0; a < arr[i].length; a++) {
                arr[i][a] = "EMPTY"; 
// you can have the sysout statement here as well instead of having looping the entire array again. 
                System.out.print(arr[i][a] + " ");    
            }
    
    // this loop must be executed separately inorder to check values present in the array or else you can have a sysout statement when assigning the "empty" value in the array.
            for (int b = 0; b < arr.length; b++) {
                for (int j = 0; j < arr[b].length; j++) {
                    System.out.print(arr[b][j] + " ");
                }
                System.out.println();
            }
        }
    }

Upvotes: 0

Tolga
Tolga

Reputation: 1

Actually for(int b) is in for(int i); that's why you observe null values. If you move for(int b) outside of for(int i), there will be no null values.

public static void arr_2d(){
    String [][] arr = new String[3][2];
    for (int i = 0; i < arr.length; i++) {
        for (int a = 0; a < arr[i].length; a++) {
            arr[i][a] = "EMPTY";
        }
    }

    for (int b = 0; b < arr.length; b++) {
        for (int j = 0; j < arr[b].length; j++) {
            System.out.print(arr[b][j] + " ");
        }
        System.out.println();
    }

}

Upvotes: 0

g00se
g00se

Reputation: 4296

Your loops are nested wrongly, which will result in the filling process not being complete while you're trying to process its results. You need

public static void arr_2d() {
    String[][] arr = new String[3][2];
    for (int i = 0; i < arr.length; i++) {
        for (int a = 0; a < arr[i].length; a++) {
            arr[i][a] = "EMPTY";
        }

    }
    for (int b = 0; b < arr.length; b++) {
        for (int j = 0; j < arr[b].length; j++) {
            System.out.print(arr[b][j] + " ");
        }
        System.out.println();
    }
}

Upvotes: 1

Related Questions