1

I am trying to create a three dimensional array that outputs as:

[1,2,3], [1,2,3], [1,2,3], [1,2,3]
[1,2,3], [1,2,3], [1,2,3], [1,2,3]
[1,2,3], [1,2,3], [1,2,3], [1,2,3]
[1,2,3], [1,2,3], [1,2,3], [1,2,3]
[1,2,3], [1,2,3], [1,2,3], [1,2,3]

Here is the code I have come up with:

public class Triples {

    public static void main(String[] args) {

        int[][][] triplet = new int[5][4][3];

        for (int i = 0; i < triplet.length; i++) 
        {
            for (int j = 0; j < triplet[i].length; j++) 
            {
                System.out.print("[");
                for (int k = 0; k < triplet[i][j].length; k++) 
                {
                    triplet[i][j][k] = i+1;
                    System.out.print(triplet[i][j][k] + "," + "");
                }
                System.out.print("]");
            }
            System.out.println();
        }
    }
}

The result I have looks like:

[1,1,1,][1,1,1,][1,1,1,][1,1,1,]
[2,2,2,][2,2,2,][2,2,2,][2,2,2,]
[3,3,3,][3,3,3,][3,3,3,][3,3,3,]
[4,4,4,][4,4,4,][4,4,4,][4,4,4,]
[5,5,5,][5,5,5,][5,5,5,][5,5,5,]

My issue is I am not sure how to increment the values in the third for loop to go as 1, 2, and 3. I have tried different combinations, but they have resulted with either the same or incorrect result. Any tips?

3 Answers 3

3

Simply assign the value of k+1. That's all

Explanation:(I am not very good at this.) Values of i and j are not going to change in the k loop. So you don't need them. But the value ok k does change in every iteration. Also it will reset to 0 every time. But we don't want 0 so change it to k+1. Do put some debug println in your statement. They help a lot.

triplet[i][j][k] = k+1;

With this the new inner most for loop looks like:

for (int k = 0; k < triplet[i][j].length; k++) 
{
    triplet[i][j][k] = k+1;

    //Don't put the comma after last digit.
    if(k==triplet[i][j].length-1) {
        System.out.print(triplet[i][j][k] );
    }else {
        System.out.print(triplet[i][j][k] + "," + "");
    }

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

5 Comments

Wow. That seemed a little too easy. Thank you! Can you maybe explain the reason behind this? I just want to make sure I understand.
That made sense, no worries. Thank you very much.
As I said put some println statements in every loop. Use Eclipse IDE if you are not using it. It has a very good feature called Debugging, google that.
Yeah eclipse works very well, still learning the debugging feature. Also, just got the privilege to up vote!
Welcome to Beautiful world of Java and Stackoverflow. All the best!
0

You need to initial the value of adder in the second loop and then add the value by 1 inside the deeper loop. Try this:

public class Triples {

    public static void main(String[] args) {

        int[][][] triplet = new int[5][4][3];

        for (int i = 0; i < triplet.length; i++) 
        {
            for (int j = 0; j < triplet[i].length; j++) 
            {
                System.out.print("[");
                for (int k = 0; k < triplet[i][j].length; k++) 
                {
                    triplet[i][j][k] = k+1;
                    System.out.print(triplet[i][j][k] + "," + "");
                }
                System.out.print("]");
            }
            System.out.println();
        }
    }
}

1 Comment

What does a++ refer to?
0
public class Triples {

    public static void main(String[] args) {

        int[][][] triplet = new int[5][4][3];

        for (int i = 0; i < triplet.length; i++) 
        {
            for (int j = 0; j < triplet[i].length; j++) 
            {
                System.out.print("[");
                for (int k = 0; k < triplet[i][j].length; k++) 
                {
                    triplet[i][j][k] = k+1;
                        if(k==triplet[i][j].length-1) 
                            System.out.print(triplet[i][j][k] );
                        else
                            System.out.print(triplet[i][j][k] + ",");
                }
                if(j==triplet[i].length-1) 
                    System.out.print("]");
                else
                    System.out.print("], ");
            }
            System.out.println();
        }
    }
}

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.