+ 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 Answers
+ 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.