Created
December 24, 2021 08:18
-
-
Save gauravkukade/0738e623abfab4de8781fbde33b805bb to your computer and use it in GitHub Desktop.
A java program to check if the ArrayList contains the specified element. Read more at https://coderolls.com/arraylist-contains-method/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* A java program to check if the ArrayList | |
* contains the specified element. | |
* | |
* @author Gaurav Kukade a coderolls.com | |
*/ | |
public class ArrayListContainsMethodExample { | |
public static void main(String[] args) { | |
//create an empty arraylist object 'states' | |
List<String> states = new ArrayList<String>(); | |
//add state in the arraylist | |
states.add("California"); | |
states.add("Texas"); | |
states.add("Florida"); | |
states.add("New Jersey"); | |
states.add("Washington"); | |
//check if states contains florida | |
boolean isPresent1 = states.contains("Florida"); | |
System.out.println("Is Florida present in the list: "+ isPresent1); | |
//check if states contains alaska | |
boolean isPresent2 =states.contains("Alaska"); | |
System.out.println("\nIs Alaska present in the list: "+ isPresent2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Is Florida present in the list: true | |
Is Alaska present in the list: false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment