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) + " ");
}
}
}
}
}
(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) + " ");
}
}
}
}
}
相关文章推荐
- MySQL一次执行多条语句的实现及常见问题(C API Commands out of sync; you can't run this command now)
- mysql 出现 Commands out of sync; you can't run this command now
- 解决error:2014 Commands out of sync; you can't run this command now
- mysql: Commands out of sync; you can't run this command now
- python Commands out of sync; you can't run this command now
- Can't create a new thread (errno 11); if you are not out of available memory, you can consult the manual for a possible OS-dependent bug
- 解决error:2014 Commands out of sync; you can't run this command now
- PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
- ERROR (ProgrammingError) (2014, "Commands out of sync; you can't run this command now")
- LR错误: Commands out of sync; you can't run this command now 解决方法
- php用mysql_query()调用存储过程后无法再执行sql报错:commands out of sync you can't run this command now
- error:2014 Commands out of sync; you can't run this command now
- MySQL一次执行多条语句的实现及常见问题(C API Commands out of sync; you can't run this command now)
- 053-23 How can you reverse the effects of an ALTER DISKGROUP … DROP DISK command if it has not yet c
- 解决error:2014 Commands out of sync; you can't run this command now
- Commands out of sync; you can't run this command now
- CareerCup How to find medium of 1 billion numbers across N distributed machines efficiently?
- Commands out of sync; you can't run this command now SQL
- PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now
- 解决mysql error:2014 Commands out of sync; you can't run this command now