Search code examples
javajava-melcdui

Determining the index of an Item on a Form (J2ME)


Given an Item that has been appended to a Form, whats the best way to find out what index that item is at on the Form?

Form.append(Item) will give me the index its initially added at, but if I later insert items before that the index will be out of sync.


Solution

  • This was the best I could come up with:

    private int getItemIndex(Item item, Form form) {
        for(int i = 0, size = form.size(); i < size; i++) {
            if(form.get(i).equals(item)) {
                return i;
            }
        }
        return -1;
    }
    

    I haven't actually tested this but it should work, I just don't like having to enumerate every item but then there should never be that many so I guess its ok.