简体   繁体   English

在使用remove方法之前/之后重新填充arrayList的效果

[英]Effects of re-populating an arrayList before/after using remove method

Why does re-populating an array before using the remove method affect the contents after using remove method? 为什么在使用remove方法之前重新填充数组会影响使用remove方法之后的内容?

I am trying to remove all items from an ArrayList (element by element) starting with the highest index. 我试图从最高索引开始的ArrayList(逐元素)中删除所有项目。 The reason for copying the array is so that I can consistently reference an identical data set in other code examples later on (using methods that modify the array) which are not shown here. 复制数组的原因是为了使我以后可以在其他代码示例中(使用修改数组的方法)始终引用相同的数据集(此处未显示)。

Why is the output different for both of these code examples? 为什么这两个代码示例的输出都不同?

    //populate arrrayList
    for (int i = 0; i<1000; i++ ){
        int randNum = rand.nextInt(1000);
        arrayList.add(randNum);
    }
    //create a identical arrayList for testing purposes
    for (int i = 0; i<1000; i++){
        testArrayList.add(arrayList.get(i));

    // ...some other arrayList accessor methods here 

    //re-populate the testArrayList with original random numbers
    for (int i = 0; i<1000; i++){
        testArrayList.add(arrayList.get(i));
    }
    System.out.println("Contents of al: " + arrayList);
    System.out.println("Contents of tal: " + testArrayList);
    System.out.println();

    // ArrayList remove (low index to high index)
    for (int i = 0; i < 1000; i++) {
        testArrayList.remove(0);
    }
    System.out.println("Contents of al: " + arrayList);
    System.out.println("Contents of tal: " + testArrayList);
    System.out.println();

In the above version the output shows both arrays contain the exact same elements - (unexpected). 在以上版本中,输出显示两个数组都包含完全相同的元素-(意外)。

In the following code I remove (what I thought was just a redundant) re-populating loop and the output shows that the second array titled "testArrayList" is empty (as expected). 在下面的代码中,我删除了(我认为只是多余的)重新填充循环,并且输出显示标题为“ testArrayList”的第二个数组为空(符合预期)。

    //populate arrrayList
    for (int i = 0; i<1000; i++ ){
        int randNum = rand.nextInt(1000);
        arrayList.add(randNum);
    }
    //create a identical arrayList for testing purposes
    for (int i = 0; i<1000; i++){
        testArrayList.add(arrayList.get(i));

    }
    System.out.println("Contents of al: " + arrayList);
    System.out.println("Contents of tal: " + testArrayList);
    System.out.println();

    // ArrayList remove (low index to high index)
    for (int i = 0; i < 1000; i++) {
        testArrayList.remove(0);
    }
    System.out.println("Contents of al: " + arrayList);
    System.out.println("Contents of tal: " + testArrayList);
    System.out.println();

I figured that the re-population loop in the first code example was just a redundant process and would copy the contents of arrayList to testArrayList BEFORE removing elements from the testArrayList. 我认为第一个代码示例中的重新填充循环只是一个冗余过程,在将元素从testArrayList中删除之前,会将arrayList的内容复制到testArrayList中。 Why does it appear that the loop in the first example repopulates testArrayList AFTER the code that follows it? 为什么在第一个示例中的循环之后的代码之后似乎重新填充了testArrayList? I expected both of these code examples to spit out the same results. 我希望这两个代码示例都能吐出相同的结果。 Can anyone please explain why the first example is ineffective at removing the items in testArrayList? 谁能解释为什么第一个示例无法有效删除testArrayList中的项目? Thanks. 谢谢。

The below code adds 1000 elements from arrayList to testArrayList 下面的代码将1000个元素从arrayList添加到testArrayList

//create a identical arrayList for testing purposes
for (int i = 0; i<1000; i++){
    testArrayList.add(arrayList.get(i));

The below code again adds the same 1000 elements from arrayList to testArrayList . 下面的代码再次将相同的1000个元素从arrayListtestArrayList This is not re-population , it is adding more to the array. 不是 re-population ,而是在阵列中添加了更多内容

//re-populate the testArrayList with original random numbers
for (int i = 0; i<1000; i++){
    testArrayList.add(arrayList.get(i));
}

Re-population will be using the set method instead of the add method. Re-population将使用set方法而不是add方法。

//re-populate the testArrayList with original random numbers
for (int i = 0; i<1000; i++){
   testArrayList.set(i,arrayList.get(i));
}

Now, when you remove the 1000 items from testArrayList , the list would be empty. 现在,当您从testArrayList删除1000个项目时,该列表将为空。

Incorporating @JBNizet comments, The best way to add all the elements of one collection to the list would be to call the addAll() method, which appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. 合并@JBNizet注释,将一个集合的所有元素添加到列表的最佳方法是调用addAll()方法,该方法将指定集合中的所有元素追加到此列表的末尾,顺序为它们由指定集合的​​Iterator返回。

The best way to remove all the items from a list would be to call the clear() method of the arraylist which Removes all of the elements from this list. 从列表中删除所有项目的最好方法是调用arraylistclear()方法,该方法从该列表中删除所有元素。 The list is guaranteed to be empty after this call returns. 该调用返回后,该列表保证为空。

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

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