0

Hello fellow programmers!

I will post here the whole code i have. What I am trying to get is a full list of what's inside the array. I figured out how to do it with for loop and .length.

What I am asking here is: Is there any other way to print whole array?

Code starts here:

double[][][] skuskaTroj = new double[][][] {
        {
                { 12.44, 525.38, -6.28, 2448.32, 632.04 }, // [0][][]
                { -378.05, 48.14, 634.18, 762.48, 83.02 },
                { 64.92, -7.44, 86.74, -534.60, 386.73 } 
        },{
                { 48.02, 120.44, 38.62, 526.82, 1704.62 }, // [1][][]
                { 56.85, 105.48, 363.31, 172.62, 128.48 },
                { 906.68, 47.12, -166.07, 4444.26, 408.62 } 
        },{
                { 27, 263, 37.43, 26874, 5547 }, // [2][][]
                { 53, 978, 264, 338, 25287 },
                { 18765, 222, 363.28, 225.29, 12345 }, 
        }
};
for (int x = 0; x < skuskaTroj.length; x++) {
    for (int y = 0; y < skuskaTroj.length; y++) {
        for (int z = 0; z < 5; z++) {
            System.out.println("Hodnota je: " + skuskaTroj[x][y][z]);
        }
    }
}

Also, as you can see, in last for loop I used z < 5 because z < skuskaTroj.length didnt worked for me. It did not print the whole list. the 4th and 5th number in each line was skipped. Any idea why?

Thanks ;)

2 Answers 2

1

real 3-dimensional arrays don't exist in Java. It's just an array of array of array.

for (int x = 0; x < skuskaTroj.length; x++){
  for (int y = 0; y < skuskaTroj[x].length; y++){
    for (int z =0; z < skuskaTroj[x][y].length; z++){

Of course you should also check for null if it's possible that any sub-arrays might be null.

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

Comments

1

Because your array is 3 x 3 x 5 fields, skuskaTroj.length is 3 (the first part). If you want to refer to the actual array, the first loop end condition is y < skuskaTroj[x].length and the second z < skuskaTroj[x][y].length.

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.