0

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.

4
  • Minimal sample code please Commented Nov 10, 2014 at 22:46
  • Really, for your own good, read the javadoc of List and ArrayList. It has a copy constructor, an addAll() method, and a clear() method. Removing from the beginning in a loop is extremely inefficient: it forces the list to copy all the elements again and again from index to index - 1. Commented Nov 10, 2014 at 22:48
  • Actually, that is the purpose of this exercise. I am demonstrating just that - that certain ways of accessing and modifying contents of an ArrayList are more efficient than others. I have removed the code that I am using to time each type of accessor loop in order to minimize the amount of code and focus on my actual question. Commented Nov 10, 2014 at 23:08
  • @JB Nizet Speaking of efficiency, why would it be slightly more efficient to access an arrayList from low to high with get() than it would be to access the same arrayList from high to low using get() ....does this have to do with the efficiency of incrementing vs decrementing? Commented Nov 10, 2014 at 23:56

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Incorporated that comment into the answer. That's the better approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.