简体   繁体   English

将一维数组转换为二维数组

[英]Converting 1 dimensional array to 2 dimensional array

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();

    }
}

你在找吗

String[][] twoDimensional = new String[][]{one, two, three, four, five, six};
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 

Maybe you a looking for a 3 dimensional array, check the following: 也许您正在寻找3维数组,请检查以下内容:

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 

As suggested by Ahmed , you could also represent your cube as a tridimensional array. Ahmed所建议,您还可以将多维数据集表示为三维数组。

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. 这将替换您的onetwo等数组。

Arrays are not that hard to understand. 数组并不难理解。
Imagine a box, and that would be your everyday variable. 想象一个盒子,那将是您的日常变量。 String s , would be an example. String s ,将是一个示例。

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] . 也就是说,您告诉您的程序在该行中将多少个盒子绑在一起(数组length ),然后通过它们的编号访问各个盒子,例如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] . 为了简单起见, 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). 现在让我们尝试三个维度(我将无法为该维度写出很棒的ASCII艺术)。

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. 即使它不包含在此问题的范围内,但如果您坚持使用Java,则稍后您将希望了解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. 然后,您可以指定Color类型作为枚举。

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). 使用字符串时,如果键入“ rde”而不是“ red”,则程序将继续运行,并且只有在为时已晚时( 程序已经在运行并打印这些错误值),您才会注意到它。

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. enum的主要优点是,如果您键入Color.RDE ,则编译器会警告您,并且只有在修复该问题后才编译您的程序,这是一件很高兴的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM