+ 2

I have a known object property value, how to find the index in the collection

Student a=new Student(“jack”) Student b=new Student("Lily") list<Student> list=new ......; list.add(a); list.add(b); now i want to move "jack",it can use like this, list.remove(0); but sometimes i dont know where the index of " jack" in the list ,how can i find it and remove it ,can you help me ?i think 3hours over!

2nd Oct 2018, 6:28 PM
xuyexu
xuyexu - avatar
2 odpowiedzi
+ 4
You get the index of the element from the indexOf method. Or you just pass the object to remove to the remove method. Both work fine. A comment on your code: List is an interface, you can't instantiate an interface, the right side needs a concrete class, like this: List<Student> students = new ArrayList<>(); Interface on the left side, implementation on the right side.
2nd Oct 2018, 7:12 PM
Tashi N
Tashi N - avatar