I have a known object property value, how to find the index in the collection | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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