Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can i use contains() to check if an element i m trying to insert in an Arraylist is already there?

protected void addTeacher(Teacher t){
if (!(teacherList.contains(t)))
teacherList.add(t);
}

Thanks!

What I have tried:

Searching concrete examples in websites related to coding
Posted
Updated 7-Dec-18 5:18am

Quote:
Searching concrete examples in websites related to coding

You should learn to use Google by yourself.
here is the first answer: Java.lang.String.contains() Method Example[^]
 
Share this answer
 
An important thing to remember when using methods like contains(), indexOf() is that these sort of methods rely on using the equals() method to compare object types.

In your scenario, you're trying to see whether a list contains an instance of Teacher, so you may need to consider overriding the equals() method to check for equality in the objects. This page[^] has a good description.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900