-3

I want to create 2D array of objects implemented by interface Field, but it wont run trough compile. The error which i get is : java.lang.ArrayIndexOutOfBoundsException: 9 on the line where i assign this.field[i][j] = this.border. Theese are my files:

Board.java:

package board;

public class Board {
private int size;
private Field [][] field;
private BoardField board;
private  BorderField border;

    public Board(int size){
    this.field = new Field[size+1][size+1];
    this.size = size;

    for(int i = 0; i <= size +1; i++){
        for(int j = 0; i <= size +1; j++){
            if(i == 0 || j == 0 || (i == size +1) || (j == size +1)){
                this.border =  new BorderField();
                this.field[i][j] = this.border;
            }
            else {
                this.board = new BoardField(i, j);
                this.field[i][j] = this.boar;
            }
        }
     }
  }

   public static void main (String[] args)
   {
       Board board = new Board(8);
   }
}

BoardField.java:

package board;

public class BoardField implements Field {
boolean diskOnField;
int row;
int col;

    public BoardField(int row, int col){
    this.row = row;
    this.col = col;
    diskOnField = false;
    }
}

BoardField.java:

package board;

public class BorderField implements Field {
public BorderField(){}
}

Field.java:

package board;

public interface Field {
    public static enum Direction {
        L,LU,U,RU,R,RD,D,LD;
    }
}
nocturne
  • 536
  • 3
  • 7
  • 31
  • First `int j = 0; i <= size +1; j++` contains a typo. Then an array index goes from 0 to `length - 1`. But you are looping until `length` here, because of the `<=`. – Tunaki Mar 04 '16 at 12:58
  • thank you for the typo, but i am not sure why u talk about length since I am no using it there. What i wanted to do is that there would be BorderField-s on the border of array, and BoardField-s in the center of array – nocturne Mar 04 '16 at 13:14
  • Either way, you can't access an array out of its bounds. Please read the linked question. – Tunaki Mar 04 '16 at 13:15

1 Answers1

0

You assigned this.field = new Field[size+1][size+1];, so this.field[size+1] is out-of-range and you cannot access it without throwing an exception.

Maybe you want this: (change size+1 to size)

for(int i = 0; i <= size; i++){
    for(int j = 0; i <= size; j++){
        if(i == 0 || j == 0 || (i == size) || (j == size)){
            this.border =  new BorderField();
            this.field[i][j] = this.border;
        }
        else {
            this.board = new BoardField(i, j);
            this.field[i][j] = this.boar;
        }
    }
 }
MikeCAT
  • 61,086
  • 10
  • 41
  • 58
  • size+1 is there on purpose, on the borders i want to create BorderField objects, and in the center i want there to be BoardField objects – nocturne Mar 04 '16 at 13:10
  • 1
    Then increase the number of elements of the array. – MikeCAT Mar 04 '16 at 13:11
  • "Maybe you want this" is not a good and helpful answer. The linked duplicate explains this better. – Tunaki Mar 04 '16 at 13:16
  • Increasing the number of elements helped...but I an not sure I must have put there `this.field = new Field[size+2][size+2]` . For example we got field of size 8x8, then why `this.field = new Field[size+1][size+1]` should not work for me since the size will be 9, and I can put there 10 objects (2 for Borders), field[0]...field[9]= 10 objects. Can you please explain this? – nocturne Mar 04 '16 at 13:26
  • 1
    Because you can access elements of array if its index is in allowed range and you cannot access "elements" of array if its index is not in allowed range. – MikeCAT Mar 04 '16 at 13:28