下面提供的代码执行一项任务,即获取所有 x
和 y
或类似字符串,并将其添加到数组中。
我可以将它们全部添加到数组中,但我想要的是检查我要插入数组中的变量是否已经存在,然后不要插入其他插入。
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/