[jdom-interest] Fencepost error in PartialList

Elliotte Rusty Harold elharo at metalab.unc.edu
Tue May 1 14:49:06 PDT 2001


There's an off-by-one error in the addAll(Collection c) method in
PartialList. It is currently:

    public boolean addAll(Collection c) {
        if (backingList.isEmpty()) {
            return addAll(0, c);
        } else {
            return addAll(backingList.indexOf(getLast()), c);
        }
    }

The problem is that this puts the objects from the Collection objects
the last element rather than after it. 

We need to change getLast() to getLast()+1; that is:

    public boolean addAll(Collection c) {
        if (backingList.isEmpty()) {
            return addAll(0, c);
        } else {
            return addAll(backingList.indexOf(getLast())+1, c);
        }
    } 

There might be simpler solutions as well. For instance, I suspect the
following might work as well:

    public boolean addAll(Collection c) {
        return backingList.addAll(c);
    }

However, since it isn't clear to me why the extra code was there in the
first place, I don't feel comfortable making this change myself.

-- 
+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+ 
|               Java I/O (O'Reilly & Associates, 1999)               |
|            http://metalab.unc.edu/javafaq/books/javaio/            |
|   http://www.amazon.com/exec/obidos/ISBN=1565924851/cafeaulaitA/   |
+----------------------------------+---------------------------------+
|  Read Cafe au Lait for Java News:  http://metalab.unc.edu/javafaq/ | 
|  Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/     |
+----------------------------------+---------------------------------+



More information about the jdom-interest mailing list