简体   繁体   English

使用像c ++迭代器这样的Java迭代器

[英]using java iterators like c++ iterators

    Vector <String> songList;
    Iterator<String> begin= songList.iterator();
    Iterator<String> end= songList.iterator();

Say I have a vector list of songs just like the list in itunes. 假设我有一个矢量歌曲清单,就像iTunes中的清单一样。 How would I set the begin iterator to the beginning and the end iterator to the ending without iterating through the whole list? 如何在不迭代整个列表的情况下将begin迭代器设置为开始,将end迭代器设置为结束? Additionally how would I move the end iterator in the reverse direction until I meet a certain criteria? 另外,在满足特定条件之前,我将如何反向移动最终迭代器? The best I could find online was the hasnext/next functions but those are forward only? 我可以在网上找到的最好的功能是hasnext / next函数,但是这些仅是转发的? I'm new to java and tried searching for the answer but I don't know what key terms to search for. 我是java的新手,并尝试搜索答案,但我不知道要搜索哪些关键术语。 In essence what I am trying to do is narrow down a list of songs from a thousand to a few by keeping a pointer to the beginning and ending of the songs that start with a pattern. 本质上,我想通过保持指向以某种模式开头的歌曲的开头和结尾的指针,将歌曲列表从一千个缩小到几个。 So if I am searching for "the" I want to return a lower bound iterator and upper bound iterator for all the songs that start with "the". 因此,如果我要搜索“ the”,我想为以“ the”开头的所有歌曲返回一个下限迭代器和一个上限迭代器。 Then I want to keep adjusting these iterators as the user goes from typing "the" to "ther" to "there"... etc. I am aware that java doesn't use pointers like c++ uses. 然后,我想在用户从键入“ the”到“ ther”再到“ there”时不断调整这些迭代器。等等。我知道java不会像c ++那样使用指针。 I am just trying to do what I said in the fastest way possible. 我只是想尽我所能尽快做到。 Please help! 请帮忙!

From the description of what you want to do, you don't really want to use a list at all: what you want is a TreeSet . 从要执行的操作的描述中,您实际上根本不需要使用列表:您想要的是TreeSet You can use the subSet(from,to) method to search for songs that start with a pattern. 您可以使用subSet(from,to)方法搜索以模式开头的歌曲。

Either way, given a list, you can obtain an iterator that points to the last item of the list with: 无论哪种方式,给定一个列表,您都可以使用以下方法获得指向列表最后一项的迭代器:

ListIterator<String> end= songList.iterator(songList.size()-1);

Note that it points to the last item, not to one item past the last. 请注意,它指向最后一项, 而不是最后一项之后的一项。 You can iterate backwards using the previous() and hasPrevious() methods defined in the ListIterator interface . 您可以向后遍历使用previous()hasPrevious()中定义的方法ListIterator接口

Also, you should use the ArrayList class instead of Vector . 另外,您应该使用ArrayList类而不是Vector As you can read in Vector's documentation, 您可以在Vector的文档中阅读,

Unlike the new collection implementations, Vector is synchronized. 与新的集合实现不同,Vector是同步的。 If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector. 如果不需要线程安全的实现,建议使用ArrayList代替Vector。

Iterator object is used for iterating for forward direction and only for once. 迭代器对象用于向前迭代,并且只能迭代一次。 While Listiterator obj is used for iterating forward and backward direction, as well as we can iterate object more than once. Listiterator obj用于迭代向前和向后的方向,并且我们可以多次迭代对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM