我有一个方法,我遍历数组中的每个元素以检查某个字符串中是否有某个值,如果是,我将获取具有该字符串要检查的内容的相应对象并开始操作它。可以想象,此迭代开始要花费很多时间,因此我很好奇是否有人知道更好的方法。代码如下所示:
在代码中,rec是一个巨大的字符串(认为是XML文件),我们正在检查该字符串中是否包含rfids数组中的任何rfids,如果找到匹配项,则开始在其中操作线程数组池(是Thread [])并执行操作。我的问题是,有没有比遍历每个元素更好的方法,或者比使用for循环更快的方法?
for(int x = 0;x<arraySize-1;x++)
{
Thread.sleep(10);
if(rec.contains(rfids[x])) //Very resource intensive, but iterates through the arrayList to see which thread has the RFID associated with
// the received message
{
// pool.get(x).stopTime();
// System.out.println(pool[x].toString());
try {
((SendingThread) pool[x]).stopTime();
}
catch(IllegalStateException e)
{
Thread.sleep(10);
//e.printStackTrace();
}
output.append('\n' + pool[x].toString() + '\n'); //buffered output
//double difference = ((end - startTimes[x]) / 1e6) - (multiple * 10); //calculated roundtrip time (something is def wrong with the formula here)
String difference = ((SendingThread) pool[x]).getTime();;
output.append(difference + '\n'); //More output
count++; //Means we got a hit, one less request to expect
multiple = 0; //reset the multiple offset
// pool.get(x).close();
pool[x].interrupt();
break;
}