允许保持这种简单。我一直在尝试许多小时,无法找到解决方案。如何存储在矩阵格式2D阵列(用逗号)
我从MySQL数据库检索以下数据。数据库采用当前格式。
|number1|number2|number3|number4|number5|
| 0 | 1 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 | 0 |
我有以下代码检索它(工作正常):
int[][] array = new int[5][5];
int i = 0;
while(result.next()) {
array[i][0] = result.getInt(1);
array[i][1] = result.getInt(2);
array[i][2] = result.getInt(3);
array[i][3] = result.getInt(4);
array[i][4] = result.getInt(5);
for(int j=0;j<5;j++){
System.out.print(array[i][j]);
}
i++;
}
然而,返回数组的时候,我希望它是在下面的格式(例如用逗号和在正确的地方大括号,这样我可以分析此二维数组(直接使用变量“阵列”,做与其他对象请求时直接使用它)
int[][] array = {
{ 0, 1, 0, 1, 0 },
{ 1, 0, 1, 0, 1 },
{ 0, 1, 0, 1, 0 },
{ 1, 0, 1, 0, 1 },
{ 0, 1, 0, 1, 0 },
};
编辑:为了更清楚: 假设我有另一个名为secondaryArray [] []的变量。我想将从数据库检索到的数组分配给secondaryArray,但是使用当前格式,它不会让我,因为内容不是矩阵格式。
谢谢你的时间和帮助!晴赞赏
不其返回类似于自动,只是未格式化一样吗?这是你问的问题吗? –
@RileyCarney感谢您的回复莱利。目前,它是通过system.out.print函数完成的。我希望它确保数组变量可以像我显示的最后一个例子那样格式化,用逗号和大括号括起来。对不起,如果听起来很混乱。 – user1423798
好的,更清楚的是,我有另一个名为secondaryArray [] []具有相同内容的变量。我想将从数据库检索到的数组分配给secondaryArray,但以当前格式,它不会让我。 – user1423798