0

I would like to request some help converting this code to a 2 dimensional array. I'm not asking for a fix to the code, just a starting point or something since arrays are really my weak point in coding. Here is the code:

import java.io.*;
import java.util.*;

public class rubix
{
    public static void main(String[] args)
    {
        String[] one = {"red","red","red","red","red","red","red","red","red"};
        String[] two = {"blue","blue","blue","blue","blue","blue","blue","blue","blue"};
        String[] three = {"yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow"};
        String[] four = {"green","green","green","green","green","green","green","green","green"};
        String[] five = {"orange","orange","orange","orange","orange","orange","orange","orange","orange"};
        String[] six = {"white","white","white","white","white","white","white","white","white"};

        //Output each side of the rubix cube
        output(one, 1);
        output(two, 2);
        output(three, 3);
        output(four, 4);
        output(five, 5);
        output(six, 6);

    }

    //Output function, will output first the num

    public static void output(String[] side, int num)
    {
        int i,j;
        int x = 0;
        System.out.println("Side: "+num);

        for(i = 0; i < 3; i++)
        {
            for(j = 0; j < 3; j++)
            {
                System.out.print(side[x]+"\t");
                x++;
            }
            System.out.println();

        }

        System.out.println();
        System.out.println();

    }
}
4
  • 1
    It would be nice to have a 3 dimentional array since it represents a rubik cube Commented May 14, 2013 at 13:32
  • +1 that would be fun to get it. Rubik's cube in a Java application. Commented May 14, 2013 at 13:35
  • I don't know how to do 3 dimension, this is only my first year with arrays.. Commented May 14, 2013 at 13:38
  • @user1965245: Note that there is no such thing in Java as multidimensional arrays, just arrays of arrays, or arrays of arrays of arrays, etc. That is, If you have a String[][], each element of the array is an array itself that can be constructed and initialized to different sizes. It can even be that some elements of the "outer" array are nil's. So do not rely on a String[][] being an NxM matrix... Commented Jan 14, 2014 at 13:58

4 Answers 4

3

Are you looking for

String[][] twoDimensional = new String[][]{one, two, three, four, five, six};
Sign up to request clarification or add additional context in comments.

2 Comments

Where would I add this in? Before the one dimensional strings?
If you add it before it won't compile, because the variables are unknown at that point...
1
String a[][]={
        {"red","red","red","red","red","red","red","red","red"},
        {"blue","blue","blue","blue","blue","blue","blue","blue","blue"},
        {"yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow"},
        {"green","green","green","green","green","green","green","green","green"},
        {"orange","orange","orange","orange","orange","orange","orange","orange","orange"},
        {"white","white","white","white","white","white","white","white","white"}       
};

// some examples    
System.out.println(a[0][0]); // red    
System.out.println(a[3][0]); // green 

1 Comment

Now I'm getting errors for my output, saying that the outputs arent statements
0

Maybe you a looking for a 3 dimensional array, check the following:

public static void main(String[] args) {
    String[][][] rubik={
            {
                {"red","red","red"},
                {"red","red","red"},
                {"red","red","red"}
            },{             
                {"blue","blue","blue"},
                {"blue","blue","blue"},
                {"blue","blue","blue"}
            },{
                {"yellow","yellow","yellow"},
                {"yellow","yellow","yellow"},
                {"yellow","yellow","yellow"}
            },{
                {"green","green","green"},
                {"green","green","green"},
                {"green","green","green"}
            },{
                {"orange","orange","orange"},
                {"orange","orange","orange"},
                {"orange","orange","orange"}
            },{
                {"white","white","white"},
                {"white","white","white"},
                {"white","white","white"}
            }
    };

    output(rubik, 0);
    output(rubik, 1);
    output(rubik, 2);
    output(rubik, 3);
    output(rubik, 4);
    output(rubik, 5);
}

public static void output(String[][][] rubik, int num)
{
    int i,j;
    int x = 0;
    System.out.println("Side: "+num);

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            System.out.print(rubik[num][i][j]+"\t");
            x++;
        }
        System.out.println();

    }

    System.out.println();
    System.out.println();

}

To clarify a little bit:

rubik[s][c][r]

s=side
c=column on the side s
r=row on the side s 

Comments

0

As suggested by Ahmed, you could also represent your cube as a tridimensional array.

The solution with two dimensions, will go along the lines of previous answers.

String[][] cube = {
    {"red","red","red","red","red","red","red","red","red"},
    {"blue","blue","blue","blue","blue","blue","blue","blue","blue"},
    {"yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow"},
    {"green","green","green","green","green","green","green","green","green"},
    {"orange","orange","orange","orange","orange","orange","orange","orange","orange"},
    {"white","white","white","white","white","white","white","white","white"}
}

This would replace your one, two, and so on, arrays.

Arrays are not that hard to understand.
Imagine a box, and that would be your everyday variable. String s, would be an example.

Awesome ASCII variable representation:
    [«content»]

An array, in this analogy, would be a zero-indexed line of boxes. That is, you tell your program how many boxes are tied together in that line (the array length), and then you access the individual boxes by their number, for instance, a[index].

Awesome ASCII array representation:
    [«content»][«content»][«content»] ... [«content»]
       Box 0      Box 1      Box 2       Box (length-1)

In a two dimensional array, you now have lines and columns of boxes. Or, in other words, you have a matrix of boxes, or a rectangle of boxes, whatever you prefer. You access the individual elements by two indexes. For isntance, a[line][column].

Awesome ASCII matrix representation:
    Lines/Columns   0    1    2    3    4    ...
                0  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                1  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                2  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                3  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                4  [ ]  [ ]  [ ]  [ ]  [ ]   ...
               ...

The above resembles a square, or a face of your cube.

Let's try now with three dimensions (I won't be able to write awesome ASCII art for that one).

String[][][] cube = {
    // First face, a square, or a two-dimensional array
    {
       // First line
       {"red", "red", "red"},
       // Second line
       {"red", "red", "red"},
       // Third line
       {"red", "red", "red"}
    },
    // Second face
    {
       // First line
       {"blue", "blue", "blue"},
       // Second line
       {"blue", "blue", "blue"},
       // Third line
       {"blue", "blue", "blue"}
    },
    // Do the same for the four remaining faces.
}

With the above, you can access every single little square with ease.
Suppose, upon a rotation, that I want to change the three right vertical squares.

// For face f (0 .. 5), change 3rd column (2), in every line (0, 1, 2).
cube[f][0][2] = newcolor;
cube[f][1][2] = newcolor;
cube[f][2][2] = newcolor;

If you're interested in more, keep reading. If this suits your needs, you may stop reading here.


Even though it is not included in the scope of this question, if you stick with Java, later on you will want to learn about Enumerations.

Enumerations allow you to specify a fixed set of values, for later use. In your cube, the colors are a fixed set of values that you know beforehand (the colors are always the same six). You could then specify you Color type, as an enumeration.

public enum Color {
    RED, BLUE, ORANGE, GREEN, YELLOW, WHITE
}

Instead of using Strings, you may now use your own colors. For instance, let's take the above example of a rotation in a tridimensional array, and assign the color red to face zero.

cube[0][0][2] = Color.RED;
cube[0][1][2] = Color.RED;
cube[0][2][2] = Color.RED;

This may seem as a lot to take in, for a beginner, and that's why I've put this in a different section of my answer.

When using Strings, if you type "rde", instead of "red", your program will go on, and you'll notice it only when it's too late already (ie, your program is already running and printing these erroneous values).

The main advantage with enum, is that if you type Color.RDE, your compiler will warn you, and won't compile your program until you fix that, which is a nice thing to have.

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.