-1

我想在 for 循环中用“String row”的变量填充我的“String [] collect”数组,但我不知道如何正确地做到这一点!当数组被填充时,我想将它显示为带有 textview 的字符串。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    List<double[]> lokal = readCsv(getApplicationContext());

    for (double[] sor : lokal) {

        for (int i = 0; i < sor.length; i++) {
            mean(sor);
            String row = Double.toString(mean(sor));
            String[] collect = new String[] { row };
        }
    }

    getActionBar().setDisplayHomeAsUpEnabled(true);

    TextView textView = new TextView(this);
    textView.setTextSize(30);
    textView.setText(collect);

    setContentView(textView);
}
4

1 回答 1

3

String Array每次都通过您的loop. 使用 anArrayList因为它们可以具有动态大小并尝试类似

   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   List<double[]> lokal = readCsv(getApplicationContext());

   ArrayList<String> array = new ArrayList<String>();  // initialize ArrayList here, may want to declare as class variable

   for (double[]sor : lokal) {
    for(int i=0; i<sor.length; i++) {
       mean(sor);
       String row = Double.toString(mean(sor));
       array.add(row);   // add your String to it here       

数组列表文档

要将所有Strings 放在一起,只需遍历它们即可。就像是

String result = ""
for (int c=0; c<array.size(); c++)
{
    String curString = array.get(c);
    result.concat(curString);
}
textview.setText(result);

如果这不起作用,您可以使用

result = result + curString;

因为“+”是Strings的连接运算符

于 2013-06-18T18:06:31.890 回答