我想知道如何将我拥有的数组放入textView

时间:2015-05-06 00:28:22

标签: java android arrays

这是我做完一些工作后的代码。如何将三个阵列放入将在手机上显示的文本视图中。我基本上必须在数组中创建随机数,将它们加起来并且它是第三个数组。

private int[] arrayOne =  new int[5], arrayTwo = new int[5], arrayThree = new int[5];

public int randomNumbers(){
    Random rand = new Random();
    return rand.nextInt(20) -10;  
}

public void generateArrays(){
    for(int i = 0; i < arrayOne.length; i++) {
        arrayOne[i] = randomNumbers();
        arrayTwo[i] = randomNumbers();    
    }
}

public void arraySum(){
    for(int i = 0; i < arrayOne.length; i++){
        int temp = arrayOne[i] + arrayTwo[i];
        if (temp < 0) temp = 0;
            arrayThree[i] = temp;
    }    
}

1 个答案:

答案 0 :(得分:0)

您提供的代码并不使用TextView。所以,我们只能猜出你想要展示的内容。也许是这样的:

  public String arraySum(){
        StringBuilder sb=new StringBuilder();
        for(int i = 0; i < arrayOne.length; i++){
            int temp = arrayOne[i] + arrayTwo[i];
            if (temp < 0) temp = 0;
            arrayThree[i] = temp;
            sb.append(arrayOne[i]).append("+").append(arrayTwo[i]);
            sb.append("=").append(temp).append("\n");
        }
        return sb.toString();    
    }
...
TextView text = (TextView) findViewById(R.id.text);
test.setText(arraySum());