3

I've got an object of type DocObject that contains an arraylist of DocObjects within it called children, each of which may or not contain children themselves. I'm writing a function for this object called replace() that takes a child to be searched for, and if the DocObject contains that child then the child should be replaced with newObj. I have looked around the site and searched google but nothing I've seen is working. The code below shows what I've tried:

public void replace(DocObject oldObj, DocObject newObj) {
    for (DocObject child : children ) {
        if (child == oldObj) {
            child = newObj;
        }
    }
}

And this (this causes an overflow exception):

public void replace(DocObject oldObj, DocObject newObj) {
    if (children.indexOf(oldObj) != -1)
        children.set(children.indexOf(oldObj), newObj);
    for (DocObject child : children)
        child.replace(oldObj, newObj);
}

This isn't replacing the child, however, and I have checked to see that the if statement is working correctly and its condition met. How can I replace oldObj with newObj?

1
  • Why do you need the loop to begin with? Why can't you use List#indexOf directly? May use something like while (children.indexOf(oldObj) != -1) {...} Commented Mar 5, 2014 at 4:23

1 Answer 1

1

I would not recommend looping using the for (X x: thingWithXs) construct while manipulating the list. I would recommend using indexOf to search for the desired object and if it cannot be found, recursively calling replace on the children of the object you are looking at.

Note that you'll have to modify your replace method to accept the list of objects as an argument:

public boolean replace(List<E> list, E oldE, E newE) {
    if (list == null) {
        return false;
    }
    int index = list.indexOf(oldE);
    if (index > 0) {
        list.set(index, newE);
        return true;
    }
    for (int i = 0, l = list.size(); i < l; i++) {
        List<E> children = list.get(i).children;
        if (replace(children, oldE, newE)) {
            return true;
        }
    }
    return false;
}

Disclaimer: The above code has not been tested. It should give you an idea as to how it can be done. Essentially what it comes down to is checking if the element you are looking for is in the list, and if not, iterating over the list and checking each set of children.

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

5 Comments

+1 for suggesting the use of indexOf. An enhanced for loop would be useful for iterating through the children for the recursion.
something like this? hold up tabbing doesnt show up. ill edit into op
@kylecblyth updated answer to give you a better idea of what I think.
still got some issues, but I'm sure I'll be able to fix em up with some tinkering. Thanks!
Do take note that indexOf() relies on the underlying object's equals() method, and if your object does not implement equals(), the List will use the default Java Object.equals().

Your Answer

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