ArrayList question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

ArrayList question

How can I write a method which take parameter from an user by Scanner and then remove it from ArrayList. For example Array "Competitors" has a class "Person" and I would like to write lastName parameter in the console and remove it from the list by competitors.remove(...).

28th Aug 2018, 1:56 PM
Jakub ⚡
Jakub ⚡ - avatar
5 Answers
+ 5
Can you show your code?😊
28th Aug 2018, 1:59 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 1
In order to use competitors.remove() you need to give it a person object, and this must be considered equal to the element you want to remove, so you need to do this: Create your Person class and override the equals() method to tell it how you consider two people equal (same first and last name?) Your scanner can take in strings from the user and you can use these to construct a person instance. This means your Person class should have a constructor that takes your desired properties. Should be good to go with this :)
28th Aug 2018, 8:38 PM
Dan Walker
Dan Walker - avatar
29th Aug 2018, 7:52 AM
Jakub ⚡
Jakub ⚡ - avatar
0
Thanks a lot. It's an answer on my question. I will try tomorrow and tell you about results.
28th Aug 2018, 9:18 PM
Jakub ⚡
Jakub ⚡ - avatar
0
How do I play around with codes. .....seriously. ..started learning java last week but wen I try out......I get code error am puzzled. ..at least I want to create two. ....because things are confusing me.
4th Sep 2018, 10:52 AM
NAMOMBWE HALIMAH
NAMOMBWE HALIMAH - avatar