+ 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ïŒ
2 RĂ©ponses
+ 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.