hash算法取两数组中的相同元素

public static List<String> getSame2(String[] array_1,String[] array_2)
{
Arrays.sort(array_1);
Arrays.sort(array_2);

//相同元素集合
List<String> sameList = new ArrayList<String>();

int j = 0;
int k = 0;

int compareResult = 0;
while(j < array_1.length && k< array_2.length)
{
/*
*比较两个值大小
*若是相等,则加入集合,反之则变动小一方的下标值
*/
compareResult = array_1[j].compareTo(array_2[k]);
if (0 == compareResult)
{
sameList.add(array_1[j]);
k ++;
j ++;
}
else if (0 < compareResult)
{
k ++;
}
else
{
j ++;
}
continue;
}
return sameList;
}

发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>