Hi there,
I’ve got some menu structure where I’m nesting ArrayLists like this class Menu extends ArrayList<Menu>
. It gives me some really interesting results when I’m using indexOf
. Somehow it looks like it’s doing a recursive indexOf
(traversing down the tree). Is this normal? What do you think?
When I override the indexOf
function with my own code it works as I would expect.
Menu root = new Menu();
void setup() {
Menu a = root.addItem("a");
Menu b = root.addItem("b");
Menu c = root.addItem("c");
Menu d = root.addItem("d");
Menu b1 = b.addItem("b1");
Menu b2 = b.addItem("b2");
Menu b3 = b.addItem("b3");
Menu b2x = b2.addItem("b2x");
Menu b2y = b2.addItem("b2y");
Menu b2z = b2.addItem("b2z");
println(root.indexOf(b2z)); //returns 0 instead of -1
println(b.indexOf(b2z)); //returns 0 instead of -1
println(b2.indexOf(b2z)); //returns 0 instead of 2
}
class Menu extends ArrayList<Menu> {
String title;
Menu addItem(String title) {
Menu m = new Menu();
m.title = title;
add(m);
return m;
}
//when overriding indexOf it works like I would expect.
//int indexOf(Menu findMe) {
// for (int i=0; i<size(); i++) {
// if (get(i)==findMe) return i;
// }
// return -1;
//}
}