如何检查数组列表是否包含特定字符串 [英] How do i check if an Array List contains a certain string

查看:114
本文介绍了如何检查数组列表是否包含特定字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里查看过,但是我得到的并没有真正起作用.这是运行的代码,但是没有按照我的期望做

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

    }

}

推荐答案

您不必这样做:

if(Arrays.asList(list).contains("Paul"))

因为标识符list已经是ArrayList

您需要这样做:

if(list.contains("Paul")){
    System.out.println("Yes it does");
}

这篇关于如何检查数组列表是否包含特定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆