简体   繁体   中英

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?

I am trying to remove all items from an ArrayList (element by element) starting with the highest index. 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).

    //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. Why does it appear that the loop in the first example repopulates testArrayList AFTER the code that follows it? 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? Thanks.

The below code adds 1000 elements from arrayList to 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 . This is not re-population , it is adding more to the array.

//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-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.

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.

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. The list is guaranteed to be empty after this call returns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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