简体   繁体   English

二维ArrayList中的ArrayList.clear()(ArrayLists的ArrayList)

[英]ArrayList.clear() in a two dimensional ArrayList(ArrayList of ArrayLists)

So I'm having some issues with adding ArrayLists to my ArrayList. 所以我在将ArrayLists添加到ArrayList时遇到了一些问题。 Think of this as a table. 将此视为表格。

Here's some example code: 这是一些示例代码:

 ArrayList<String> currentRow = new ArrayList<String>(); 

  while ((myLine = myBuffered.readLine()) != null) {

    if(rowCount == 0) {// get Column names  since it's the first row

        String[] mySplits;
        mySplits = myLine.split(","); //split the first row

        for(int i = 0;i<mySplits.length;++i){ //add each element of the splits array to the myColumns ArrayList
            myTable.myColumns.add(mySplits[i]);
            myTable.numColumns++;
            }
        }
    else{ //rowCount is not zero, so this is data, not column names.
    String[] mySplits = myLine.split(","); //split the line
    for(int i = 0; i<mySplits.length;++i){

    currentRow.add(mySplits[i]); //add each element to the row Arraylist

    }
    myTable.myRows.add(currentRow);//add the row arrayList to the myRows ArrayList
    currentRow.clear(); //clear the row since it's already added
        //the problem lies here *****************
     }
    rowCount++;//increment rowCount
    }
 }

The problem is when I don't call currentRow.clear() to clear the contents of the ArrayList that I'm using in each iteration (to put into my ArrayList of ArrayList), with each iteration, I get that row PLUS every other row. 问题是当我不调用currentRow.clear()来清除每次迭代中要使用的ArrayList的内容(放入ArrayList的ArrayList中)时,每次迭代时,我都会得到该行加上其他行。

But when I do call currentRow.clear() after I add currentRow to my arrayList<ArrayList<String> , it actually clears the data that I added to the master arrayList as well as the currentRow object.... and I just want the currentRow ArrayList empty but not the ArrayList that I just added to my ArrayList (Mytable.MyRows[currentRow]). 但是,当我打电话currentRow.clear()后,我加入currentRow到我arrayList<ArrayList<String> ,它实际上清除,我添加到主ArrayList中还有currentRow对象的数据....我只是想currentRow ArrayList为空,但不是我刚刚添加到ArrayList(Mytable.MyRows [currentRow])的ArrayList。

Can anyone explain what's going on here? 谁能解释这是怎么回事?

The problem lies here: 问题出在这里:

myTable.myRows.add(currentRow);

You add the ArrayList currentRow to the "master" list here. 您将ArrayList currentRow添加到此处的“主”列表中。 Note that under Java semantics, you are adding a reference to the currentRow variable. 请注意,在Java语义下,您要添加对currentRow变量的引用

On the next line, you immediately clear currentRow : 在下一行,您立即清除currentRow

currentRow.clear()

Hence, when you try to use it later, the "master" list looks up that reference from before and finds that while there is an ArrayList object, it contains no String s within it. 因此,当您稍后尝试使用它时,“主”列表会从以前开始查找该引用,并发现尽管存在ArrayList对象,但其中没有String

What you really want to do is start over with a new ArrayList , so replace the previous line with this: 您真正想做的是从一个新的 ArrayList ,所以用以下内容替换上一行:

currentRow = new ArrayList<String>();

Then the old object is still referred to by the "master" list (so it will not be garbage collected) and when it is accessed later, its contents will not have been cleared. 然后,旧对象仍由“主”列表引用(因此不会被垃圾回收),以后再访问它时,其内容将不会被清除。

Don't clear the current row, instead create a completely fresh ArrayList for each row, inside your outer loop. 不要清除当前行,而是在外循环中为每行创建一个完全新鲜的ArrayList。

When you add currentRow to the list, you're adding a reference to the list, not a copy that will continue to exist independently. 当将currentRow添加到列表时,是在添加对列表的引用,而不是将继续独立存在的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM