Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

primitive problem with arrays.

Options
  • 25-11-2007 9:13pm
    #1
    Registered Users Posts: 26,579 ✭✭✭✭


    three threads in as many weeks, i think i've lost my programming skills :(.

    i have two arrays
    private int[] array_of_five = new int[5]; // stores 5 random int values.
    
    private int[] array_of_seven = new int[6];
    
    the array_of_five can only store numbers between 1 and 6.
    the array_of_six is set up to count how many 1's, 2's ... 6's. there are in the array_of_five.

    so i have a simple function like this
    public void function(){
           for(int i = 0 ; i < array_of_six.length ; i++)
                     array_of_six[i] = 0 ;
           
           java.util.Arrays.sort(array_of_five);
    
           for(int i = 0 ; i < array_of_six.length ; i++)
                         array_of_six[ array_of_five[i] ]++;
           }
    }
    
    basically if array of five is sorted and looks like this {1,1,3,4,6} i want array_of_six to look like this {2,0,1,1,0,1} eg (two 1's) (no 2's) (one 3) (one 4) (no 5's) (one 6).

    i'm getting an Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 and it points me to the line in the above code that is
    array_of_six[ array_of_five[i] ]++;
    

    am i going about filling array_of_six the wrong way totally?


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Well you are going out of bounds because array_of_five can only index from 0 - 4 and array_of_six goes from 0 - 5. Now in the last for loop you are using i to go from 0 - 5, but using i in the array_of_five so you will end up trying to access array_of_five[5] when it only goes up to index 4 since it is 5 in length.


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,079 Mod ✭✭✭✭AlmightyCushion


    array_of_five only has five elements in it but you do the loop 6 times. On the sixth iteration of the loop you say array_of_five. i will be equal to 5 at this stage so you are trying to access the sixth element in array_of_five which doesn't exist.


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Cremo wrote: »
    three threads in as many weeks, i think i've lost my programming skills :(.

    i have two arrays
    private int[] array_of_five = new int[5]; // stores 5 random int values.
    
    private int[] array_of_seven = new int[6];
    
    the array_of_five can only store numbers between 1 and 6.
    the array_of_six is set up to count how many 1's, 2's ... 6's. there are in the array_of_five.

    so i have a simple function like this
    public void function(){
           for(int i = 0 ; i < array_of_six.length ; i++)
                     array_of_six[i] = 0 ;
           
           java.util.Arrays.sort(array_of_five);
    
           for(int i = 0 ; i < array_of_six.length ; i++)
                         array_of_six[ array_of_five[i] ]++;
           }
    }
    
    basically if array of five is sorted and looks like this {1,1,3,4,6} i want array_of_six to look like this {2,0,1,1,0,1} eg (two 1's) (no 2's) (one 3) (one 4) (no 5's) (one 6).

    i'm getting an Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 and it points me to the line in the above code that is
    array_of_six[ array_of_five[i] ]++;
    

    am i going about filling array_of_six the wrong way totally?

    What about that loose curly brace?


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    lol, the second i posted this up i reread the post and noticed my mistake.

    the second for loop should of read.
    for(int i = 0 ; i < array_of_five.length ; i++)
                         array_of_six[ array_of_five[i] ]++;
    

    case closed

    that stray curly brace was a typo :o

    thanks for the help guys, it's mucho appreciatedo


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    :)


  • Advertisement
Advertisement