Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ArrayOutOfBoundsException

    Hey guys i'm getting an ArrayOutOfBoundsException: 0

    any help would be appeciated

    Code:
    package tes;
    
    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();
    }
    }
    }
    PHP Code:
    *. 
    Attached Files Attached Files

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,799
    Thanks
    78
    Thanked 4,411 Times in 4,376 Posts
    My eyes hurt.

    You never heard of indenting code?

    But anyway, WHERE do you get the error????
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Sep 2009
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Refer to the statement mentioned below in the method run():

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

    (J*S)/T may not always be an integer . I think you should try rounding the result of the division before using it.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •