0

I'm trying to understand multidimensional arrays I added some integer values randomly iam having a problem finding the sum of two random integers as the third dimension something like this

      Random  val=new Random();

    int [][][]myloop=new int[2][2][2];
    for (int i = 0; i < myloop.length; i++) {

        for (int j = 0; j < myloop[0].length; j++) {
            for (int k = 0; k < 2; k++) {

                myloop[i][j]=val.nextInt();
            }

        }



    }     

1 Answer 1

6

You didn't add random values. You initialized an array of random dimensions.

This is probably the code you're looking for(based on the discussion below):

Random val = new Random();
int numberOfPairs=10; //example
int[][] data = new int[numberOfPairs][3];
for (int i=0;i<numberOfPairs;i++){
    data[i][0]=val.nextInt();
    data[i][1]=val.nextInt();
    data[i][2]=data[i][0]+data[i][1];
}
Sign up to request clarification or add additional context in comments.

3 Comments

But I still don't have any idea if the question is wrong or the code below is wrong. Could you try to rephrase the question to make it more clear or add some information. What should be the output?
Sure.I have three dimensional array .First dimension is for first integer value.Second is for second integer value .Finally, third dimension is for sum of these two values.I tried something like this but i cant figured out how can i do this
So the problem is that you don't understand the basic data structures(do some googling on arrays). You don't need a three-dimensional array. You need two dimensional array[n][3], where n is the number of tuples.

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.