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");
}
}
}
you don't have to do this:
because the identifier
list
is already anArrayList
you'll need to do:
ArrayList have their inbuilt function called contains(). So if you want to try with in built function you can simply use this method.
list.contains("Your_String")
This will return you boolean value true or false
The reason why you not getting what you expected is the usage of
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: