该方法要去除list的所有null,因为输入的list非常、非常大,请问如何尽可能地改进
这个方法?
public void removeAllNull(LinkedList f) {
List lock = new ArrayList();
for (int i = 0; i < f.size(); i++) {
synchronized (lock) {
if (f.get(i) == null) {
lock = new ArrayList();
f.remove(i);
}
lock.add(f);
}
}
}
输入:[1, 2, null, 3, null, 4, null, 5, null, 6]
输出:[1, 2, 3, 4, 5, 6]
还有一道题,多线程共同扫描DNA,主线程在多线程中启动scanDNA方法,每个线程都有
一个私有的dnaList,每个线程共享一个“results”对象来向主线程报告进度,
成功扫描次数必须精确。请改正和修改scanDNA方法以提高任务性能。
private interface DNA {
public boolean scan();
}
public void scanDNA(List dnaList, Properties results) {
synchronized (results) {
Iterator i = dnaList.iterator();
Object lock = new Object();
while (i.hasNext()) {
if (i.next().scan()) {
String key = "Successful Scans Performed";
synchronized (lock) {
results.setProperty(key, String.valueOf(Integer.valueOf(
results.getProperty(key)) + 1));
}
}
}
}
}