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;
}
}