2012-08-17 59 views
0

大家好我有一個2D數組。我在文本視圖上打印此數組的元素。但是我的所有元素都在一行中。我想要元素之間的換行符。我下面的代碼,並給出我的二維數組是:如何在textView中的nextline上打印數組元素android

table[][]={{1,2,3,4,5,6,7,8},{8,7,6,5,4,3,2,1}} 

for(i=0;i<8;i++) 
for(j=0;j<2;j++) 
{ 
text = new TextView(this); 
text.setText(""+table[j][i]); 
} 

和我得到的輸出是這樣的:

{1,8,2,7,3,6,4,5,5,4,6,3,7,2,8,1} 

,但我想輸出是這樣的:

1,8 
2,7 
3,6 
4,5 
5,4 
6,3 
7,2 
8,1 

任何幫助將不勝感激。

回答

0

我建議不要在環路創建的TextView。 只需使用同樣的觀點:

table[][]={{1,2,3,4,5,6,7,8},{8,7,6,5,4,3,2,1}} 
text = new TextView(this); 

for(i=0;i<8;i++) { 
    for(j=0;j<2;j++) { 
     text.append(""+table[j][i] + "\n"); 
    } 
} 
0

嘗試:

text = new TextView(this); 

for loop { 
     text.append(""+table[j][i] + "\n"); 
} 
0

您需要添加轉義序列\ n

嘗試了這一點,

table[][]={{1,2,3,4,5,6,7,8},{8,7,6,5,4,3,2,1}} 

text = new TextView(this); 
StringBuffer sb = new StringBuffer(); 

for(i=0;i<8;i++) 
for(j=0;j<2;j++) 
{ 
    sb.append ("" + table[j][i] + "\n"); 
} 

text.setText(sb.toString()); 
0

這將每兩個號碼後面添加新行。

table[][]={{1,2,3,4,5,6,7,8},{8,7,6,5,4,3,2,1}} 
String str=""; 
for(i=0;i<8;i++) 
{ 
    for(j=0;j<2;j++) 
    { 
    str=str+table[j][i]; 
    } 
    str=str+"\n"; 
} 
text = new TextView(this); 
text.setText(Html.fromHtml(str)); 
+0

它不適合我。仍然我的輸出是相同的。 – sachit 2012-08-17 08:08:26

相關問題