startsWith() 返回意外值
该课程项目要求我们读取单个文本文件中包含的 10,514 首歌曲的标题、艺术家和歌词。该项目的当前部分让我们编写一个有序的展开链接列表并在标题字段上运行搜索。还编写了比较器来按标题对列表进行排序。我们必须跟踪查找匹配项所需的比较。
在测试时,我得到了一些奇怪的结果。例如,运行搜索
angel返回23个匹配,需要552次比较,与教授给出的答案相符
t 返回零个匹配项,需要 9530 次比较,而预期为 1148 次匹配
ta 返回 62 个匹配项,需要 8455 次比较
s 返回没有匹配项,需要 8383 次比较
sa 返回 89 个匹配项并需要 7355 次比较
我的搜索算法运行如下:
- 循环遍历列表以查找第一个匹配项
- 循环遍历列表以查找第一个与搜索字段不匹配的实例
- 将开始和结束对象发送到数据结构的 Sublist 方法,该方法循环遍历这两个对象并构建单独的匹配列表
- 返回匹配列表
对于第一步和第二步,我通过if (currentSong.getTitle().toLowerCase().startsWith(titleSearch))
将当前值与搜索值进行比较
这行代码是什么使用单字母搜索返回 false 的代码,但是当添加 a 时,会找到值?最好,我想要一个不需要我在调试器中手动单步执行 8000 多次循环迭代的解决方案。此外,教授还对结构进行了预期值的测试,我的代码通过了所有测试。
The class project has us reading in the title, artist and lyrics of 10,514 songs contained in a single text file. The current section of the project had us write an ordered unrolled linked list and run a search on the title field. The comparator was also written to sort the list by title. We have to keep track of the comparisons required to find the matches
When testing, I'm getting some odd results. For example, running a search for
angel returns 23 matches and requires 552 comparisons, which matches the answer given by the professor
t returns zero matches and requires 9530 comparisons, where 1148 matches were expected
ta returns 62 matches and requires 8455 comparisons
s returns no matches and 8383 comparisons were required
sa returns 89 matches and required 7355 comparisons
My search algorithm runs like this:
- loop through the list to find the first match
- loop through the list to find the first instance that does not match the search field
- send the start and end objects to the Sublist method of the data structure, which loops through those two objects and builds a separate list of matches
- return list of matches
For both step one and two, I compare the current value to the search value viaif (currentSong.getTitle().toLowerCase().startsWith(titleSearch))
What is it about this line of code that returns false with a single letter search, but when an a is added, values are found? Preferably, I'd like a solution that wouldn't require me to manually step through 8000-odd iterations of a loop in a debugger. In addition, the professor provided tests to the structure with the expected values, and my code passed all the tests.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现问题出在哪里了。在 subList 方法中,我使用二分搜索方法来识别第一个找到的匹配项的索引位置。但是,由于二分搜索仅返回它遇到的第一个匹配项,因此我有一个循环来向后遍历数组以找到真正的第一个匹配项。
然而,在这种情况下,二分搜索返回的第一个命中位于 0 索引处,因此当我向后走时,会抛出 ArrayIndexOutOfBoundsException ,从而使整个事情短路。添加第二个测试解决了这个问题。
I found out what the problem was. In the subList method, I used a binary search method to identify the index location of the first found match. However, since binary search returns only the first match it comes across, I had a loop to walk backwards thru the array to find the real first match.
However, in this case, the first hit returned from binary search was at the 0 index, so when I walked backwards, an ArrayIndexOutOfBoundsException was thrown, thus short-circuiting the entire thing. Adding a second test solved the problem.