下面提供的代码执行的任务是获取所有xy或类似的字符串,并将其添加到数组中。

我可以将它们全部添加到数组,但是我要检查的是如果要插入数组的变量已经存在,则不要插入else insert。

ArrayList vars = new ArrayList();
for(int i = 0; i < temp.length; i++)
    for(int j = 1; j < temp[0].length - 1; j++)
    {
        String text_to_parse = temp[i][j];
        Pattern y = Pattern.compile("[?]\\w[,)]"); // find the values in pattern of ?x, or ?x)
        Matcher z = y.matcher(text_to_parse);
        while(z.find())
        {
            String to_add = z.group();
            to_add = to_add.replaceAll("[\\,\\) ]","");
            // logic required here if to_add exist in vars then do no insert else insert

        }
     }


我尝试使用vars.contain,但它会添加它找到的所有值。

最佳答案

尝试vars不包含。像这样使用!运算符

if (!(vars.contains(item)))
   //add the item
else
   //do nothing

关于java - 如何在运行时检查数组是否包含特定值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37314343/

10-11 22:16
查看更多