Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将一维数组转换为二维数组_Java_Arrays - Fatal编程技术网

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

Java 将一维数组转换为二维数组,java,arrays,Java,Arrays,我想请求一些帮助将此代码转换为二维数组。我不是在要求对代码进行修复,只是一个起点,因为数组实际上是我在编码方面的弱点。代码如下: 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

我想请求一些帮助将此代码转换为二维数组。我不是在要求对代码进行修复,只是一个起点,因为数组实际上是我在编码方面的弱点。代码如下:

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

    }
}
import java.io.*;
导入java.util.*;
公共类rubix
{
公共静态void main(字符串[]args)
{
字符串[]一={“红”、“红”、“红”、“红”、“红”、“红”、“红”、“红”、“红”、“红”、“红”};
String[]two={“blue”、“blue”、“blue”、“blue”、“blue”、“blue”、“blue”、“blue”};
字符串[]三={“黄”、“黄”、“黄”、“黄”、“黄”、“黄”、“黄”、“黄”、“黄”、“黄”};
字符串[]四={“绿色”、“绿色”、“绿色”、“绿色”、“绿色”、“绿色”、“绿色”、“绿色”、“绿色”、“绿色”};
字符串[]五={“橙色”、“橙色”、“橙色”、“橙色”、“橙色”、“橙色”、“橙色”、“橙色”、“橙色”、“橙色”};
字符串[]六={“白色”、“白色”、“白色”、“白色”、“白色”、“白色”、“白色”、“白色”、“白色”、“白色”};
//输出rubix多维数据集的每一侧
输出(一,1);
输出(二,二);
产出(三、三);
产出(四、四);
产出(五、五);
产出(六、六);
}
//输出函数,将首先输出num
公共静态void输出(字符串[]边,int num)
{
int i,j;
int x=0;
System.out.println(“侧:+num);
对于(i=0;i<3;i++)
{
对于(j=0;j<3;j++)
{
系统输出打印(面[x]+“\t”);
x++;
}
System.out.println();
}
System.out.println();
System.out.println();
}
}
你在找什么

String[][] twoDimensional = new String[][]{one, two, three, four, five, six};
你在找什么

String[][] twoDimensional = new String[][]{one, two, three, four, five, six};

如果要查找三维阵列,请检查以下内容:

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

}

如果要查找三维阵列,请检查以下内容:

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

}
正如所建议的,您还可以将多维数据集表示为三维数组

具有二维的解决方案将遵循前面的答案

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"}
}
这将替换
one
two
等数组

数组并不难理解。
想象一个盒子,那将是你每天的变量<例如,代码>字符串s

Awesome ASCII variable representation:
    [«content»]
在这个类比中,数组是一个零索引的框行。也就是说,您告诉您的程序该行中有多少个框绑定在一起(数组
长度
),然后通过它们的编号访问各个框,例如,
a[index]

Awesome ASCII array representation:
    [«content»][«content»][«content»] ... [«content»]
       Box 0      Box 1      Box 2       Box (length-1)
在二维数组中,现在有了框的行和列。或者,换句话说,你有一个长方体矩阵,或者长方体,不管你喜欢什么。您可以通过两个索引访问各个元素。对于iInstance,
a[行][列]

Awesome ASCII matrix representation:
    Lines/Columns   0    1    2    3    4    ...
                0  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                1  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                2  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                3  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                4  [ ]  [ ]  [ ]  [ ]  [ ]   ...
               ...
上面的形状类似于正方形或立方体的面

现在让我们尝试三维(我将无法为这一个编写令人敬畏的ASCII艺术)

有了上述功能,您可以轻松访问每个小广场。
假设在旋转时,我想改变三个垂直的正方形

// 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;
如果你对更多感兴趣,继续阅读。如果这符合你的需要,你可以停止阅读这里


尽管它不包括在这个问题的范围内,但如果您坚持使用Java,稍后您将希望了解枚举

枚举允许您指定一组固定的值,以供以后使用。在立方体中,颜色是预先知道的一组固定值(颜色始终相同)。然后,您可以指定
Color
type作为枚举

public enum Color {
    RED, BLUE, ORANGE, GREEN, YELLOW, WHITE
}
您现在可以使用自己的颜色,而不是使用字符串。例如,让我们以上述三维数组中的旋转为例,将红色指定给面零

cube[0][0][2] = Color.RED;
cube[0][1][2] = Color.RED;
cube[0][2][2] = Color.RED;
对于初学者来说,这似乎有很多需要理解的地方,这就是为什么我在回答的另一部分中加入了这一点

当使用字符串时,如果您键入“rde”,而不是“red”,您的程序将继续运行,并且只有在已经太晚时(即,您的程序已经在运行并打印这些错误值)您才会注意到它

enum
的主要优点是,如果您键入
Color.RDE
,编译器将警告您,并且在您修复该问题之前不会编译您的程序,这是一件好事。

正如所建议的,您还可以将多维数据集表示为三维数组

具有二维的解决方案将遵循前面的答案

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"}
}
这将替换
one
two
等数组

数组并不难理解。
想象一个盒子,那将是你每天的变量<例如,代码>字符串s

Awesome ASCII variable representation:
    [«content»]
在这个类比中,数组是一个零索引的框行。也就是说,您告诉您的程序该行中有多少个框绑定在一起(数组
长度
),然后通过它们的编号访问各个框,例如,
a[index]

Awesome ASCII array representation:
    [«content»][«content»][«content»] ... [«content»]
       Box 0      Box 1      Box 2       Box (length-1)
在二维数组中,现在有了框的行和列。或者,换句话说,你有一个长方体矩阵,或者长方体,不管你喜欢什么。您可以通过两个索引访问各个元素。对于iInstance,
a[行][列]

Awesome ASCII matrix representation:
    Lines/Columns   0    1    2    3    4    ...
                0  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                1  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                2  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                3  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                4  [ ]  [ ]  [ ]  [ ]  [ ]   ...
               ...
上面的形状类似于正方形或立方体的面

现在让我们尝试三维(我将无法为这一个编写令人敬畏的ASCII艺术)

有了上述功能,您可以轻松访问每个小广场。
假设在旋转时,我想改变三个垂直的正方形

// 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;
如果你对更多感兴趣,继续阅读。如果这符合你的需要,你可以停止阅读这里


尽管它不包括在这个问题的范围内,但如果您坚持使用Java,稍后您将希望了解枚举

枚举允许您指定