您的位置:首页 > 其它

Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if

2012-08-30 17:19 281 查看
Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list
(fixed list).









import java.util.ArrayList;

import java.util.List;

public class Duplicate {

public static void main(String args[]) {

List<Integer> list1 = new ArrayList<Integer>();

List<Integer> list2 = new ArrayList<Integer>();

for (int i = 0; i < 5; i++)

list1.add((int) (Math.random() * 10));

for (int i = 0; i < 10; i++)

list2.add((int) (Math.random() * 10));

for (int i = 0; i < list1.size(); i++) {

System.out.print(list1.get(i) + " ");

}

System.out.println();

for (int j = 0; j < list2.size(); j++) {

System.out.print(list2.get(j) + " ");

}

System.out.println();

for (int i = 0; i < list2.size(); i++) {

for (int j = 0; j < list1.size(); j++) {

if (list1.get(j) == list2.get(i)) {

System.out.print(list2.get(i) + " ");

}

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐