0

I have a method which is filling two of the three dimensions of an array.

public static String[][] Method(){
    double[][][] chromosom = new double [50][8][4];
    for(int j = 0; j < 8; j++){

        // generate random value ...

        chromosom[0][j][0] = value*2;
        chromosom[0][j][1] = value*3;
        chromosom[0][j][2] = value*5;
        chromosom[0][j][3] = value*9;
    }
}

Now I want to use this array in my main to generate 50 of these arrays and to save them all in one array.

static double[][][] chromosom = new double [50][8][4];

public static void main(String[] args){
    for(int i = 0; i < 50; i++){
        Method();
        for(int j = 0; j < 8; j++){
            chromosom[i][j][0];
            chromosom[i][j][1];
            chromosom[i][j][2];
            chromosom[i][j][3];
        }
    }
}

My problem is that I am not able to reach the chromosom array with its values from my main method.

5
  • It seems you are trying to very weirdly use the main method. You should probably avoid that. Commented Sep 9, 2016 at 14:10
  • What you want is to return a value. I would suggest doing some tutorials before jumping into your own project. thenewboston has a really good video tutorial series. Commented Sep 9, 2016 at 14:10
  • 2
    'static double[][][] = new double [50][8][4];'.. where is the variable here? Commented Sep 9, 2016 at 14:10
  • @CKing forgot it, its in now. Commented Sep 9, 2016 at 14:12
  • You're creating a local variable in your method. That has nothing to do with the field you're reading from in main. Also, you appear to be trying to use array access as a whole statement... please provide a minimal reproducible example rather than pseudo-code. Commented Sep 9, 2016 at 14:15

1 Answer 1

1

Your code doesn't compile. Your Method should return a String[][]. Why create the 3-dimension double array if this method should only create a 2-dimension String array? How about this:

public static double[][] generateXY(){
    double[][] result = new double[8][4];
    for(int j = 0; j < 8; j++){

        // generate random value ...

        result[j][0] = value*2;
        result[j][1] = value*3;
        result[j][2] = value*5;
        result[j][3] = value*9;
    }
    return result;
}

And then in your main:

public static void main(String[] args){
    double[][][] xyz = new double[50][8][4];
    for(int i = 0; i < 50; i++){
        xyz[i] = generateXY();
    }
}

Methods and variables should be renamed, I don't know what exaclty you're doing with it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.