java如何以矩阵格式存储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++;
}
但是,当返回数组时,我希望它采用以下格式(例如,在正确的位置使用逗号和花括号,以便我可以分析这个2d数组(直接使用变量“array”,并在处理其他对象的请求时直接使用它)
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,但在当前的格式中,它不允许,因为内容不是矩阵格式
谢谢你的时间和帮助!非常感谢
共 (0) 个答案