JList搜索的效率 [英] Efficiency of JList Search

查看:69
本文介绍了JList搜索的效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些Java代码,用于对JList进行前缀搜索。但是,从算法的角度来看,该算法的效率很低,它会在列表上为每个按键进行线性搜索,并在紧密循环内进行缓慢的大小写转换。

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;
    }
}


推荐答案

快速更改,考虑

String str = ((String)jList1.getModel().getElementAt(i));
str.substring(1, m_key.length()).equalsIgnoreCase(m_key);

除此之外,您自己的 startsWithIgnoreCase实现它应该快速且易于编写。

And for one step beyond that, your own implementation of startsWithIgnoreCase which should be quick and easy to write.

编辑:这似乎是在将列表滚动到与用户输入匹配的元素。您绝对应该考虑一个更复杂的数据结构。这已经做了很多,您也许可以在网上找到一些有效的算法。

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.

这篇关于JList搜索的效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆