我有一些二维数组。
int a[i][j]
我想要显示它。 我正在尝试:
String aa = "";
aa+=(a[i][j] + "\n");
table.append(aa);
但它不起作用 - 只有一个" 0"。 我怎样才能做到这一点?
答案 0 :(得分:2)
int[][] array = new int[][]{
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
};
StringBuilder builder = new StringBuilder();
for (int[] arr : array)
builder.append(Arrays.toString(arr)).append("\n");
String tableText = builder.toString();
tableText
是字符串:
"[1, 0, 0]
[0, 1, 0]
[0, 0, 1]"
假设您TextView
名为table
,您需要做的就是将此字符串设置为TextView
的文字:
table.setText(tableText);
答案 1 :(得分:2)
如果你想制作一张桌子:
int a[][] = new int[][]{{0,1,2,3,4},
{2,3,4,5,6},
{2,3,4,5,6}};
StringBuilder strBuild = new StringBuilder();
for(int i = 0; i < a[0].length; i++){
for(int j = 0; j < a.length; j++){
strBuild.append(a[i][j]);
strBuild.append(" ");
}
strBuild.append("\n");
}
yourTextVeiw.setText(strBuild.toString());
如果您尝试将所有数字放在一行中:
int a[][] = new int[][]{{0,1,2,3,4},
{2,3,4,5,6},
{2,3,4,5,6}};
StringBuilder strBuild = new StringBuilder();
for(int i = 0; i < a[0].length; i++){
for(int j = 0; j < a.length; j++){
strBuild.append(a[i][j]);
strBuild.append(" ");
}
}
yourTextView.setText(strBuild.toString());
如果您尝试将它们全部放在一列中:
int a[][] = new int[][]{{0,1,2,3,4},
{2,3,4,5,6},
{2,3,4,5,6}};
StringBuilder strBuild = new StringBuilder();
for(int i = 0; i < a[0].length; i++){
for(int j = 0; j < a.length; j++){
strBuild.append(a[i][j]);
strBuild.append("\n");
}
}
yourTextView.setText(strBuild.toString());
并且一行都被粉碎在一起:
int a[][] = new int[][]{{0,1,2,3,4},
{2,3,4,5,6},
{2,3,4,5,6}};
StringBuilder strBuild = new StringBuilder();
for(int i = 0; i < a[0].length; i++){
for(int j = 0; j < a.length; j++){
strBuild.append(a[i][j]);
}
}
yourTextView.setText(strBuild.toString());
答案 2 :(得分:1)
int a[][] = new int[][] {
{ 0, 1, 2, 3, 4, 3, 1, 1, 7, 9 },
{ 4, 5, 6, 4, 8, 6, 1, 3, 6, 7 },
{ 7, 1, 5, 7, 8, 9, 6, 3, 5, 8 },
{ 8, 2, 3, 9, 8, 7, 6, 6, 4, 9 },
{ 9, 8, 5, 1, 2, 3, 4, 5, 3, 7 }
};
String aa = "";
for(int i=0; i<5; i++)
{
for(int j=0; j<10; j++)
{
aa.concat(String.valueOf(a[i][j]));
}
}
(your text view) .setText(aa);
答案 3 :(得分:1)
你需要循环,显然:)你现在只从[i] [j]获得一个数组元素。
像
这样的东西String aa;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
aa += a[i][j];
}
}
table.setText(aa);
如果需要,可以添加换行符(“\ n”)