[英]Converting 1 dimensional array to 2 dimensional array
我想请求一些帮助将此代码转换为二维数组。 我并不是要对代码进行修复,而只是作为起点或其他事情,因为数组实际上是我编码的弱点。 这是代码:
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
也许您正在寻找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();
}
为了澄清一点:
rubik[s][c][r]
s=side
c=column on the side s
r=row on the side s
如Ahmed所建议,您还可以将多维数据集表示为三维数组。
具有二维的解决方案将遵循先前的答案。
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
等数组。
数组并不难理解。
想象一个盒子,那将是您的日常变量。 String s
,将是一个示例。
Awesome ASCII variable representation:
[«content»]
以此类推,数组将是零索引的框线。 也就是说,您告诉您的程序在该行中将多少个盒子绑在一起(数组length
),然后通过它们的编号访问各个盒子,例如a[index]
。
Awesome ASCII array representation:
[«content»][«content»][«content»] ... [«content»]
Box 0 Box 1 Box 2 Box (length-1)
在二维数组中,您现在具有框的线和列。 或者,换句话说,无论您喜欢什么,您都有一个矩阵框或一个矩形框。 您可以通过两个索引访问各个元素。 为了简单起见, a[line][column]
。
Awesome ASCII matrix representation:
Lines/Columns 0 1 2 3 4 ...
0 [ ] [ ] [ ] [ ] [ ] ...
1 [ ] [ ] [ ] [ ] [ ] ...
2 [ ] [ ] [ ] [ ] [ ] ...
3 [ ] [ ] [ ] [ ] [ ] ...
4 [ ] [ ] [ ] [ ] [ ] ...
...
上面类似正方形或立方体的面。
现在让我们尝试三个维度(我将无法为该维度写出很棒的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.
}
通过上述操作,您可以轻松访问每个小方块。
假设旋转一次,我想更改三个右垂直正方形。
// 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,则稍后您将希望了解Enumerations。
枚举允许您指定一组固定的值,以供以后使用。 在多维数据集中,颜色是一组预先知道的固定值(颜色始终是相同的六个)。 然后,您可以指定Color
类型作为枚举。
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
,则编译器会警告您,并且只有在修复该问题后才编译您的程序,这是一件很高兴的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.