-3

Here is part of my code. I want to assign random number to the matrix population[][] first, then compare the random number to a specific number ranP, if population[][] < ranP, then re-assign population[][] to 1, otherwise 0. But it shows

arrayindexoutofboundsexception 0

Need help on the issue. Thanks!

            randGen = new Random();
            double randNum = randGen.nextDouble();

            for ( int i = 0; i < 11; i++){
                for (int k = 0; k < inipopulationsize; k++){
                    for (int j = 0; j < 25; j++){



                        ranP = 0.5;
                        //TMaxtrix[i][j] = matrix[i][j];
                        System.out.println(matrix[i][j] + " ");
                        population = new double[k][j];
                        System.out.println("randNum: " + randNum);
                        population[k][j] = randNum;
                        if (randNum <= ranP){
                            population[k][j] = 1;
                        }
                        else
                            population[k][j] = 0;
                        System.out.println("population: " + population[k][j]);

                    }//j loop


                }//k loop

            }//i loop

I am learning this by myself, and not taking any classes. If this really bothers you "experts", why dont you just ignore and save your time go home watching a movie or spending more time with your family? Appreciate the help from nice people here. But shame on you who only knows sarcasm. Here is what works finally:

            randGen = new Random();
            population = new int[inipopulationsize][25];

            for ( int i = 0; i < population.length; i++){

                    for (int j = 0; j < population[i].length; j++){
                        double randNum = randGen.nextDouble();
                        ranP = 0.5;
                        if (i < 11){
                        //System.out.println(matrix[i][j] + " ");
                        }

                        if (randNum <= ranP){
                            population[i][j] = 1;
                        }
                        else
                            population[i][j] = 0;
                    //System.out.println("population index: " + i + " Dieasease index: " + j + " DI on (1) or off (0): " + population[i][j] + "");

                    }//j loop


            }//i loop
Orangeblue
  • 219
  • 1
  • 5
  • 15
  • You **have to read [ask]** – Amit Jan 28 '16 at 19:05
  • 1
    Please update your code to show how you instantiated the variables population and matrix – OYRM Jan 28 '16 at 20:00
  • It is very unclear what you are trying to do here. Please edit your question to explain clearly what your end goal is. Is it a 2D array with randomly distributed 1's and 0's or a matrix of such arrays? It is a bit hard to tell. – River Jan 28 '16 at 21:35

3 Answers3

1

I don't see a reason for having 3 loops with a 2d array.

Random randGen = new Random(); 
double randNum;
for(int i=0; i<population.length; i++){
    for(int j=0; j<population[i].length; j++){
        randNum = ranGen.nextDouble();
        if(randNum<0.5) population[i][j] = 0;
        else population[i][j] = 1.0;
    }//j loop
}//i loop
Calvin P.
  • 847
  • 1
  • 7
  • 10
1

Is the third loop because you want i 2d arrays? If so you should probably look at ArrayLists of 2d arrays.

        int inipopulationsize = 25;
        double[][] population;
        Random randGen = new Random();
        double randNum = randGen.nextDouble();
        double ranP = 0.5;// outside loops
        population = new double[inipopulationsize][25]; // out


            for (int k = 0; k < inipopulationsize; k++){
                for (int j = 0; j < 25; j++){

                    randNum = randGen.nextDouble();//i assume you want new random every time

                    if (randNum <= ranP){
                        population[k][j] = 1;
                    }
                    else
                        population[k][j] = 0;
                }//j loop
            }//k loop
System.out.println(Arrays.deepToString(population));
anaxin
  • 710
  • 2
  • 7
  • 16
1

Your issue is that you are referring to an item outside the bounds of your 2D array.

Let's take a look at your code. This line: population = new double[k][j]; declares a new 2D arrays of size kxj. Then in this line: population[k][j] = randNum; you try to reference the item in the kth column and jth row of this same 2D array. This is not legal in Java arrays.

Java arrays are 0-indexed, which means with an array of size k, your indexes range from 0 to k-1. There is no item at index k. This is why you are receiving an index out of bounds error.

Please look at this link instructing you on the basic use of Java Arrays.

The exact error arrayindexoutofboundsexception 0 appears because on your first iteration, you create a population of size 0 by 0. Then you try to access the item in column 0 and row 0, that is to say, the first item. However as your population array has 0 size, it has no space, and even the index 0 is out of bounds.


However, I am not even sure this is what you want to be doing.

You are declaring a 2D array on each iteration of your loop. If all you are trying to do is make a single array of size inipopulationsizeby25 (these are the initial values of k and j) then you need to declare this outside of these two nested loops. Perhaps even outside of the third loop, as I am not even sure what that loop is doing.

Take a loop at anaxin's answer for how to effectively assign 0's and 1's to your population array randomly. (With randP set to 0.5 you are giving each a 50% chance of appearing.)

Community
  • 1
  • 1
River
  • 7,472
  • 11
  • 47
  • 61