How do I reference a specific index of element in an ArrayList?

Let's say I initialize an ArrayList using the following code:

 ArrayList fib = new ArrayList();

    fib.add(18);
    fib.add(23);
    fib.add(37);
    fib.add(45);
    fib.add(50);
    fib.add(67);
    fib.add(38);
    fib.add(88);
    fib.add(91);
    fib.add(10);

What if I want to reference a specific index of the array. I don't want what's in the index. I want the index itself. I know this seems redundant, but it spills into another code.

To reference what's IN the index, I would do this:

 fib.temp(4);

and it would yield

50

What if I want what index it is?

Jon Skeet
people
quotationmark

I suspect you're looking for List.indexOf:

int index = fib.indexOf(45); // index is now 3

indexOf returns -1 if the value isn't found in the list.

Note that temp isn't a member of ArrayList, and if you use fib.get(4) it will return 50, not 45 - because the index is 0-based, not 1-based.

people

See more on this question at Stackoverflow