Loading all values of 2d array in java

2d arrays | java
two dimensional array java for loop
two dimensional string array in java example
two dimensional array in java example program
print 2d array java
dynamic 2d array in java
how to fill a 2d array java
2d array methods java

I'm attempting to create a 2d puzzle slider game. I created my own object called gamestate to store the parent gamestate and the new gamestate, as I plan on solving it using BFS. A sample array will look like

int[][] tArr = {{1,5,2},{3,4,0},{6,8,7}};

Which implies

[1, 5, 2, 3, 4, 0, 6, 8, 7]

To store this state I used the following for loop, which brings indexOutOfBounds exceptions.

public class GameState {
public int[][] state; //state of the puzzle
public GameState parent; //parent in the game tree

public GameState() {
    //initialize state to zeros, parent to null
    state = new int[0][0];
    parent = null;
}

public GameState(int[][] state) {
    //initialize this.state to state, parent to null
    this.state = state;

    parent = null;
}

public GameState(int[][] state, GameState parent) {
    //initialize this.state to state, this.parent to parent
    this.state = new int[0][0];
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++) {
            this.state[i][j] = state[i][j];
        }
    }

    this.parent = parent;
}

Any ideas on how to fix this?

Multidimensional Arrays in Java, Size of multidimensional arrays: The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the  Loading all values of 2d array in java . Posted by: admin October 26, 2018 Leave a comment. Questions: Answers: Tags: java. Related Posts. java – Android HTML

The problem is in the initialisation part.

this.state = new int[0][0];

This code will create a zero sized two dimentional aray. That's why you get indexOutOfBounds exceptions when you try to set values in it.

If you want to initialize your array with zeros, the correct syntax is :

this.state = {{0,0,0},{0,0,0},{0,0,0}};

see the official documentation for full reference : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Javanotes 8.1, Section 7.5 -- Two-dimensional Arrays, Java does not actually have two-dimensional arrays. In a true 2D array, all the elements of the array occupy a  In Java programming, We can use the index position to access the two dimensional array elements. Using the index, we can access or alter/change every individual element present in a two dimensional array. An index value of a Java two dimensional array starts at 0 and ends at n-1 where n is the size of a row or column. For example, if an int

In the 3rd constructor you're initializing this.state with an empty array. It has no elements and thus a length of 0. Using a for loop to access any element of this array rises an ArrayIndexOutOfBoundsException.

Since you're passing in a state as parameter, you probably want to copy it's values to the field state.

You can do this like that:

public GameState(int[][] state, GameState parent) {
    this.state = new int[state.length][];
    for (int i = 0; i < state.length; i++) {
        if (state[i] != null) {
            this.state[i] = Arrays.copyOf(state[i], state[i].length);
        }
    }

    this.parent = parent;
}

You could of course call Arrays.of(state) but this wouldn't return a deep copy of state. For every i you would have this.state[i] == state[i].


Further reading: How do I copy a 2 Dimensional array in Java?

How to declare and Initialize two dimensional Array in Java with , In this example, you can see that salutation is a 2D array but its first row has 3 elements while the second row has just one element. You can access elements of a  myNumbers is now an array with two arrays as its elements. To access the elements of the myNumbers array, specify two indexes: one for the array, and one for the element inside that array. This example accesses the third element (2) in the second array (1) of myNumbers:

How to loop over two dimensional array in Java?, You can loop over a two-dimensional array in Java by using two for loops, also known In the first iteration, all elements of the first row are processed, just like  Size of multidimensional arrays: The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int[][] x = new int[10][20] can store a total of (10*20) = 200 elements.

Two Dimensional Array in Java, Two dimensional array in java, Java 2d array, java multidimensional array, java array in java. An array is like a container that can hold a certain number of values. If you look at the above examples, two dimensional array looks like a matrix,  String array default values: null null null null null Integer array default values: 0 0 0 0 0 Double array default values: 0.0 0.0 0.0 0.0 0.0 Boolean array default values: false false false false false Reference Array default values: null null null null null. This article is contributed by Twinkle Tyagi .If you like GeeksforGeeks and would

2D Arrays in Java, Each cell of the array is a variable that can hold a value and works like any variable. As with one dimensional arrays, every cell in a 2D array is of the same type. Two-dimensional arrays often come with performance losses. The benchmark sets up two 10 by 10 arrays. It then does a load and a store in the arrays. Version 1: Here we benchmark a flattened array (a 2D array stored as a 1D array). Version 2: In this version of the code we use a real 2D array, and use 2 indexes to get an element.

Comments
  • In the 3rd constructor you're initializing state with new int[0][0]. You cannot then access any element of the array.
  • As a side note it is not [1, 5, 2, 3, 4, 0, 6, 8, 7], but [[1, 5, 2], [3, 4, 0], [6, 8, 7]]. See my answer
  • I think this.state = state = new int[3][3]; is a clearer code (which does the same as this.state = {{0,0,0},{0,0,0},{0,0,0}};). But yours is fine too. However take into account that the indexOutOfBounds is not happening in that constructor GameState() but in this one: GameState(int[][] state, GameState parent) which you do not mention in your answer.
  • You're right, new int[3][3]; would have the same effect (because the default value of the primitive type "int" is 0). I chose to post the "explicit" version because I thought it would be more pedagogic.
  • I did not mention any constructor method. Of course, he will have to correct both.