JList搜索的效率
我遇到了一些用于在 JList 上进行前缀搜索的 java 代码。然而,看看它,算法的实质是相当低效的,对每个按键使用列表的线性搜索,并在紧密循环内进行缓慢的大小写转换。
我想对于大量的数据,自定义实现的三元搜索树将是一个更有效的解决方案。然而,如果一个人追求简单的代码并且没有需要如此复杂的性能要求,那么是否有其他更简单的方法可以改进该算法而不需要大量的额外代码?
for (int i=0; i < jList1.getModel().getSize(); i++) {
String str = ((String)jList1.getModel().getElementAt(i)).toLowerCase();
if (str.startsWith(m_key)) {
jList1.setSelectedIndex(i);
jList1.ensureIndexIsVisible(i);
break;
}
}
I came across some java code for doing a prefix search on a JList. Looking at it however, the meat of the algorithm is quite inefficient, using a linear search over the list for each keypress, and doing a slow case conversion within the tight loop.
I would imagine for very large amounts of data, a custom-implemented ternary search tree would be a much more efficient solution. However, if one was striving for simple code and did not have the performance requirements necessitating such complexities, are there other more simplistic ways this algorithm could be improved without necessitating significant amounts of additional code?
for (int i=0; i < jList1.getModel().getSize(); i++) {
String str = ((String)jList1.getModel().getElementAt(i)).toLowerCase();
if (str.startsWith(m_key)) {
jList1.setSelectedIndex(i);
jList1.ensureIndexIsVisible(i);
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要进行快速更改,请考虑
更进一步,您自己的
startsWithIgnoreCase
实现应该快速且易于编写。编辑:这似乎是将列表滚动到与用户输入匹配的元素。您绝对应该考虑更复杂的数据结构。这已经做了很多,你也许可以在网上找到一些有效的算法。
For a quick change, consider
And for one step beyond that, your own implementation of
startsWithIgnoreCase
which should be quick and easy to write.EDIT: This seems to be scrolling a list to the element that matches user input. You should definitely consider a more sophisticated data structure. This is done a lot, you might be able to find some efficient algorithm on the net.
最快的优化(对于编码时间,而不是运行时间)可能是对列表进行排序,然后进行二分搜索。您需要支付一次性的搜索前期成本(如果经常使用,该成本将被摊销),然后您将从 O(n) 变为 O(log(n))。根据我的经验,二分搜索相对简单,并且使用您已有的相同数据结构。
当然,排序的 n 树结构在算法上会更快,但需要新的数据结构和更多的编码/测试时间。决定你想把时间花在哪里。
Probably the quickest optimization (for time-to-code, not time-to-run), would be to sort the list and then do a binary search. You have a one-time up-front cost for the search (which, if this is used a lot, will get amortized away) and then you'll go from O(n) to O(log(n)). In my experience a binary search is relatively simple and uses the same data structures you already have.
Of course, a sorted n-tree structure will be faster algorithmically, but would require new data structures and more time in coding/testing. Decide where you want to spend your time.
我不同意这里发布的所有答案(并且由 JB Nizet 加密)。
JList
的一种可能替代方案是 具有一列的 JTable (有或没有TableHeader
)。JTable
很好地实现了排序和过滤。I disagree with all answer posted here (and little bit encrypted by JB Nizet). A possible alternative to
JList
is JTable with one Column (with or withoutTableHeader
).JTable
has a good implementation of Sorting and Filtering.确保不要在每次迭代时浏览所有列表。
您可以在列表中进行 (n!) 次迭代,其中 (n) 就是您所需要的。
如果列表可以排序,则使用部分比较来快速得出正确答案。但是,如果您必须创建自己的搜索功能,那可能会很混乱。
Be sure to not browse all your list at each iteration.
You could do (n!) iterations in the list where (n) is all you need.
If the list can be sort, used partial comparaison to reach the correct answers quickly. However if you have to create your own search function it could be messy.