Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

ArrayList removing wrong object

807588Aug 8 2009 — edited Aug 10 2009
Folks - I've encountered a problem with Java 1.6 ver 13 but can't find the bug reports to see if a) it's been reported or b) if it is fixed in a later version. The problem is that ArrayList is removing the wrong object. If an array contains a set of strings, some of which are duplicate strings but different objects, the removal of an object where the string is duplicated removes the first occurance of the string not the object.

Any help or pointers are appreciated. Especially a pointer to the bug reports for Java so I can do a search and/or submittal.
Lori <*>

The following code produces this output:
--- After initialization ---
Index 0 is Test
Index 1 is Foo
Index 2 is bar
Index 3 is Test
--- After removal ---
Index 0 is Foo
Index 1 is bar
Index 2 is Test
import java.util.ArrayList;
public class ArrayListTest extends Object {
  public static void main(String[] args) {
    final String TEST = "Test";
    final ArrayList<Object> list = new ArrayList<Object>();
    list.add(new String(TEST));
    list.add("Foo");
    list.add("bar");
    final Object remove = new String(TEST);
    list.add(remove);    System.out.println("--- After initialization ---");
    for (int i = 0; i < list.size(); i++) {
      System.out.println("Index " + i + " is " + list.get(i));
    }
    list.remove(remove);
    System.out.println("--- After removal ---");
    for (int i = 0; i < list.size(); i++) {
      System.out.println("Index " + i + " is " + list.get(i));
    }
  }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 7 2009
Added on Aug 8 2009
12 comments
1,076 views