如何优化以下内容:
final String[] longStringArray = {"1","2","3".....,"9999999"};
String searchingFor = "9999998"
for(String s : longStringArray)
{
if(searchingFor.equals(s))
{
//After 9999998 iterations finally found it
// Do the rest of stuff here (not relevant to the string/array)
}
}
注意:longStringArray只在运行时搜索一次,不排序,并且每次运行程序都不同。
我确定有一种方法可以改善最坏情况下的性能,但我似乎找不到...
P.S.还希望有一种解决方案,其中字符串searchingFor不存在于数组longStringArray中。
谢谢。
searchingFor
做与数组中它的完全相同的操作。- 一个好的解决方案必须知道接下来你要做什么。 - vbence