我有ArrayList包含String [],如果我将新的string []添加到列表中,它还修改了以前添加的string []对象

AAn*_*kit 0 java android

我正在将字符串[]添加到Arraylist中,但是当我尝试从中检索值时,发生了一些错误.以及使用新条目更新arraylist的先前值(String [])预期输出:
第一个,第
1,2个,第
一个,第二个

电流输出:
一,二
,二,二,二

下面是代码.

   ArrayList<String[]> list = new ArrayList<String[]>();
   String[] temp ={"First","Sec"};
   list.add(temp);
   temp[0] ="1";temp[1]="2";
   list.add(temp);
   temp[0] ="one";temp[1]="two";
   list.add(temp);

   printLog(list);
Run Code Online (Sandbox Code Playgroud)

这是打印方法

   private void printLog(ArrayList<String[]> result) {
    for (int i = 0; i < result.size(); i++) {
        Log.d(TAG,"I="+i);
        String[] temp = result.get(i);
        Log.d(TAG,temp[0]+","+temp[1]+"\n");
    }

}
Run Code Online (Sandbox Code Playgroud)

任何建议.

ada*_*ost 6

您正在将数组对象的引用添加到列表中.您需要每次都创建新数组.

   String[] temp ={"First","Sec"};
   list.add(temp);
   temp=new String[2];
   temp[0] ="1";temp[1]="2";
   list.add(temp);
Run Code Online (Sandbox Code Playgroud)