2013-04-08 138 views
0

我想用隨機數填充我的數組,但我無法在TextViewer中顯示我的數組casual[] - 幫我!在文本視圖中顯示數組

LanciaB.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 

     int result = np2.getValue(); 
     int result2 = np.getValue(); 
     result = result == 0 ? 10 : result; 

     int casual[] = new int[10]; 
     for(int i=0; i < result2; i++){ 
      casual[i] = (int) (Math.random()*10); 
     } 

     tv.setText(casual.toString()); 

     } 
    }); 

回答

4

使用重載Arrays#toString(int[])陣列作爲參數的陣列。

Arrays.toString(casual) 
+0

Thx!我用你的建議,但打印的字符串不僅是數字,還包括字符和字母......是打印問題還是錯誤的填充隨機數字的方式? – MrGun 2013-04-09 10:11:07

1

您需要連接數組的元素。在Android中,你可以做這樣的事情:

tv.setText(TextUtils.join(",", casual)); 
0

我用了你的意見,但打印在弦不僅是數字,而且字符和字母......是印刷的問題(字符串編碼),或者是錯誤的方式填充隨機數字的數組?

相關問題