1

我想将一个字符串向量与另一个向量或数组进行比较。关键是,根据某些情况,我将在数组/向量中添加一些字符串。然后,我想将它与另一个向量进行比较(这已经是一个向量,我使用的方法以这种方式返回它)并将公共元素保存在一个向量中(它应该是一个向量或者可以稍后转换)。

我不知道我是否可以使用向量来做到这一点,或者我是否应该将我的向量转换为另一种类型(数组、列表)来进行比较。有任何想法吗?

4

2 回答 2

0

你可以尝试这样的事情:

Vector commonItems = new Vector();
Iterator vector1 = (whateverThefirstvectoris).itr();
Iterator vector2 = (whateverThesecondvectoris).itr();

while(vector1.hasNext())
{
    Object temp = new Object;
    temp = vector1.next();
    while(vector2.hasNext())
    {
        if(vector2.next().equals(temp))
        {
            commonItems.add(temp);
        }
    }
}
于 2013-09-11T13:11:45.563 回答
0

要提取常见元素,您可以使用Vector.retainAll()

仅保留此 Vector 中包含在指定 Collection 中的元素。换句话说,从这个 Vector 中移除所有不包含在指定 Collection 中的元素。 返回:如果此 Vector 由于调用而改变,则返回 true

v1.retainAll(v2) //v1 will contain common elements only after this

旁注:它.equals()在引擎盖下使用

于 2013-09-11T13:01:37.130 回答