arrays - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at test.GameOfLife.main(gameOfLife.java:10) -
here code
class gameoflife { public static void main(string[] args) { int height = 0; int width = 0; int [][][] universe = new int [2][20][20]; (height=0; height <= 5; height ++){ (width=0; width <= 5; width++){ universe [2][height][width]= 0; } } system.out.print(universe[2][2][3]); } }
exception in thread "main" java.lang.arrayindexoutofboundsexception: 2 @ test.gameoflife.main(gameoflife.java:10)
when define universe
int [][][] universe = new int [2][20][20];
it has 2 spots, index 0 , index 1. should do
int depth; (depth=0; depth < universe.length; depth ++) { (height=0; height < universe[depth].length; height ++){ (width=0; width < universe[depth][height].length; width++){ universe [depth][height][width]= 0; } } }
Comments
Post a Comment