PDA

View Full Version : Array Index Out of Bounds Exception 0


chinamanmatt
04-27-2009, 07:06 AM
Hey, i'm getting an ArrayIndexOutofBoundsException : 0

Can anyone explain and help me correct this problem. I'm getting the error on the bolded line

thanks guys


import java.lang.*;
import java.util.concurrent.*;

class TDLife {
static CyclicBarrier barrier;
public static void main(String args[]) {


final int T = Integer.parseInt(args[0]);
final int N = Integer.parseInt(args[1]);
final int G = Integer.parseInt(args[2]);
final int R1 = Integer.parseInt(args[3]);
final int R2 = Integer.parseInt(args[4]);
final int R3 = Integer.parseInt(args[5]);
final int R4 = Integer.parseInt(args[6]);
final int M = Integer.parseInt(args[7]);
String[][][] arr = new String[N][N][N];

for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
for(int k = 0; k < N; k++)
arr[i][j][k] = ".";

if(N == 1)
arr[0][0][0] = "O";

else if(N == 2) {
arr[0][0][0] = "O";
arr[0][0][1] = "O";
arr[0][1][0] = "O";
}

else {
arr[0][0][0] = "O";
arr[0][0][1] = "O";
arr[0][0][2] = "O";
}

class NewThread extends Thread {

private String currentCells[][][];
private String copies[][][];
private int J;

public NewThread(String cells[][][], int t) {
J = t;
currentCells = cells;
copies = new String[N][N][N];

for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
for(int k = 0; k < N; k++)
copies[i][j][k] = ".";
}

public void copyArray(String cells[][][]) {
currentCells = cells;
}

public void run() {
int S = N*N*N;

for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
for(int k = 0; k < N; k++) {

int C = (i*(N*N)) + (j*N) + k;

if(((M == 0) &&(((J*S)/T <= C) && (C < ((J+1)*S)/T)))
|| ((M == 1) && (C%T == J))) {

int on = 0;

for (int i1 = -1; i1 < 2; i1++) {
for (int j1 = -1; j1 < 2; j1++) {
for (int k1 = -1; k1 < 2; k1++) {
int ic = i+i1;
int jc = j+j1;
int kc = k+k1;

if(ic < 0)
ic = N - 1;
if(jc < 0)
jc = N - 1;
if(kc < 0)
kc = N - 1;
if(ic == N)
ic = 0;
if(jc == N)
jc = 0;
if(kc == N)
kc = 0;

if(currentCells[ic][jc][kc] == "O")
on++;
}
}
}

if((currentCells[i][j][k] == ".") && ((on >= R1) && (on <= R2)))
currentCells[i][j][k] = "O";
else if ((currentCells[i][j][k] == "O") && ((on >= R4) && (on <= R3)))
currentCells[i][j][k] = ".";
}
}
}
}

try {
barrier.await();
}
catch (InterruptedException ex) {
return;
}
catch (BrokenBarrierException ex) {
return;
}
}

public void copyOver() {

for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
for(int k = 0; k < N; k++)
currentCells[i][j][k] = copies[i][j][k];
}
}

final NewThread [] threadArray;

threadArray = new NewThread[T];

for(int i = 0; i < T; i++)
threadArray[i] = new NewThread(arr, i);

barrier = new CyclicBarrier(T, new Runnable () {
public void run() {
for(int i = 0; i < T; i++)
threadArray[i].copyOver();
}
}
);

for(int i = 0; i < G; i++) {
for(int j = 0; j < T; j++)
threadArray[j].start();

barrier.reset();
}
}
}

servlet
04-27-2009, 12:32 PM
This is because you called the program without passing any command line argument,