Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i check if an Array List contains a certain string

Tags:

java

oop

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");
        }

    }

}
like image 638
papa Avatar asked Apr 12 '17 01:04

papa


People also ask

How do you check if an ArrayList contains a string in Java?

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.

How do I check if a string contains a particular list?

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.

How do you search for an element in an ArrayList?

An element in an ArrayList can be searched using the method java. util. ArrayList. indexOf().

How to check if element is present in ArrayList of strings?

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?

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.

What is the difference between contains () and contains () in ArrayList?

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.

How to check if an array contains an empty element?

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.


2 Answers

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");
}
like image 136
Ousmane D. Avatar answered Oct 09 '22 00:10

Ousmane D.


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");
 }
like image 21
Mikhail Chibel Avatar answered Oct 09 '22 00:10

Mikhail Chibel