Java ArrayList.remove () does not reduce the size of the ArrayList array

I have an ArrayList to store some data, but whenever I remove an item from the list, the size does not decrease, even when I call ArrayList.trimToSize (). This causes me a nullPointerException.

How to remove one element from ArrayList and reduce the size of the list ()?

EDIT: Alright, here is the code. Here is some background that you need to know, since I cannot publish all the code. I have an ArrayList called _dataHeap and a HashMap called _dataMap. ArrayList is a binary heap containing a findable object with a key. The HashMap binds to the key with the index of the object in the ArrayList. This is so that an element in the queue can be found by element using a HashMap or by index using an ArrayList. A key can be any object if it is unique for each element in the queue.

I debugged this line by line, and the heap contains an object, right down to Hashcode. The problem is that the object is never deleted from the ArrayList. This should mean that _dataMap.get (element.getKey ()) does not indicate where it should be. I tested it, although I used a test object outside of my implementation, which maps from String to a custom object using String as a key.

I am making one object with the string "one" as its key. I insert it and then try to delete it. I went through this, and everything checks, except for one: the object is never deleted from the queue. He received the same Hashcode, the same Key, that's all. It is removed from the map just fine, but not from the ArrayList.

Here's the remove method:

public T remove(T element) {
    //We'll need this data to return the proper value
    T t = _dataHeap.get(_dataMap.get(element.getKey()));
    /*
     * this Swap() call is used to swap our target with the end
     * of the arraylist. This means that whenever we remove it,
     * we don't have a change in indexes of the other nodes.
     * After that, we downHeapify() to fix the whole graph back
     * to it functional state.
     */
    swap(_dataMap.get(element.getKey()),length()-1);
    //Remove from the Heap
    _dataHeap.remove(_dataMap.get(element.getKey()));
    _dataHeap.trimToSize();
    //Remove from the Map
    _dataMap.remove(element.getKey());
    downHeapify();
    return t;

Hope this gives you a better idea of ​​what I'm doing wrong.

: , , , ! _dataHeap.get(element.index) . !

+5
2

Bemace, , , . , equals() , , , , .

, , equals, hashCode. SO, HashMaps.:)

. JUnit. , , - , . .

+6

, . remove?

remove(int), . remove(Object), . . , , , null false.

+4

All Articles