I've looked on here and what i got did not really work. Here is the code which runs but it's not doing what i expect it to do
package bcu.gui;
import java.util.ArrayList;
import java.util.Arrays;
public class compare {
private static ArrayList<String> list = new ArrayList<String>();
public static void main(String[] args) {
list.add("Paul");
list.add("James");
System.out.println(list); // Printing out the list
// If the list containsthe name Paul, then print this. It still doesn't print even though paul is in the list
if(Arrays.asList(list).contains("Paul")){
System.out.println("Yes it does");
}
}
}
To check if ArrayList contains a specific object or element, use ArrayList. contains() method. You can call contains() method on the ArrayList, with the element passed as argument to the method. contains() method returns true if the object is present in the list, else the method returns false.
contains() in Java. ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Returns: It returns true if the specified element is found in the list else it returns false.
An element in an ArrayList can be searched using the method java. util. ArrayList. indexOf().
Since, the element is present in the HashSet, contains () method should return true. In this example, we will define a ArrayList of Strings and add some elements to it. The we shall check if element "f" is present in this ArrayList using contains () method. Since, the element is not present in the ArrayList, contains () method should return false.
How to Check if ArrayList contains a Specific Object in Java? To check if ArrayList contains a specific object or element, use ArrayList.contains () method.
ArrayList.contains () returns true if this list contains the specified element/object. If the element is not present in this ArrayList, then contains () returns false. The element whose presence in the ArraList has to be found. The method returns boolean value.
Arrays.asList (array).contains ("EMPTY") returns true until one element changes to something that isn't "EMPTY". Why? The array still contains "EMPTY". Convert your array to a List and than use the contains method. List mylist = Arrays.asList (directions); mylist.contains (input); true if the list contains the specified element.
you don't have to do this:
if(Arrays.asList(list).contains("Paul"))
because the identifier list
is already an ArrayList
you'll need to do:
if(list.contains("Paul")){
System.out.println("Yes it does");
}
The reason why you not getting what you expected is the usage of
Arrays.asList(list)
which returns a new array with a single element of type array. If your list contains two elements [Paul, James], then the Arrays.asList(list) will be [[Paul, James]].
The correct solution for the problem already provided by 'Ousmane Mahy Diaw'
The following will also work for you:
// if you want to create a list in one line
if (Arrays.asList("Paul", "James").contains("Paul")) {
System.out.println("Yes it does");
}
// or if you want to use a copy of you list
if (new ArrayList<>(list).contains("Paul")) {
System.out.println("Yes it does");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With