我的大脑有点酸痛,我没办法指责为什么这行不通。
我有两个arraylist
private ArrayList<String> name = new ArrayList<String>();
private ArrayList<String> name2 = new ArrayList<String>();
我需要检查“名称”中的值是否包含“名称2”中的值,如果是,则进行迭代,这是我当前的代码:
private ArrayList<Integer> getForeignKey() {
ArrayList<Integer> foreignKey = new ArrayList<Integer>();
for (int i = 0; i < name.size(); i++) {
int intForeignKey = 1;
for (int x = 0; x < name2.size(); x++)
//System.out.println(name.get(i) + " ---------------- " + name2.get(x));
if (!name.get(i).contains(name2.get(x)))
intForeignKey++;
else
break;
foreignKey.add(intForeignKey);
}
return foreignKey;
}
打印出来后,它可以很好地适用于几个值,然后开始跳过数字,因此输出为:
012345510
当应该是
0 1 3 4 5 5 6 7 8 8 8 9 10
我究竟做错了什么?如果需要更多说明,我会尽力而为。
编辑:
请注意,上面的数字只是输出看起来像的示例数字。
名称包含:
a,b,c,d,e,f,g
name2包含
a,b,c,c,d,e,e,f,g,g
name(index i)检查是否包含name2(index x)值,如果包含该值,则不递增外键整数,如果不包含该值,则递增外键整数。
最佳答案
您是否要查找两个集合中相同的名称?
private final Set<String> names = new LinkedHashSet<String>();
private final Set<String> names2 = new LinkedHashSet<String>();
public Set<String> namesInBoth() {
Set<String> ret = new LinkedHashSet<String>(names);
ret.retainAll(names2);
return ret;
}