迭代通过集合 [英] Iterating through a collection
问题描述
我试图创建hasnext()有一个迭代器的next()方法,使得集合的输出将是:
print,elements,in,order,from,collection
输入:
[A] - [printing,elements,in]
[B] - [order]
[C] - [from,collection]
目前我的方法如下:
MyIterator(Collection< Collection< Object>> myColl){
_myColl = myColl;
}
public boolean hasNext(){
if(myColl.next!= null)
{
return true
}
return!queue.isEmpty();
}
public Object next()throws java.util.NoSuchElementException {
//不知道这里放什么... 。
}
p>
好吧,我不明白你的问题真的..为什么你不能只使用正常的迭代器?
我会告诉你如何创建一个基本的迭代器,让你了解基本的工作原理,然后适应你的解决方案:
假设我们需要遍历List< T>您想要创建一个帮助类来完成它。
class ListIterator< T&
您需要两个私人字段
- 要迭代的列表
- 指向最后一个项目的指针
<和3个方法+1构造函数
- hasNext() - > Boolean,如果有更多项目需要迭代,则返回true
- next() - >返回列表中的下一个元素
- reset() - >重置interal指针
< >
如何查看字段?
private final List< T>列表; //该调用将迭代的列表
private int pointer; //指向下一个可迭代项的指针
如描述中所述,构造函数将引用到列表中,所以它只是
public ListIterator(List< T> list)
{
this.list = list;
pointer = 0;
}
保存引用并将指针设置为0(开始)。
让我们来谈谈方法:
hasNext
当前指针已经达到列表的大小。
所以它只是(pointer!= list.size())
public boolean hasNext()
{
return(pointer!= list.size());
}
如果有更多项目可用,则为true,否则为false。
next
返回下一个项目(如果有)。可以通过使用我们的hasNext方法简化,因此它将
public T next()
{
if(!hasNext())
throw new NoSuchElementException(no field);
return list.get(pointer ++);
}
注意事项:
- T是返回,因为我们的列表是类型T
- list.get(pointer ++)我们首先从位置
指针
,然后我们将1加到指针
复位方法只是一个 pointer = 0
。
public void reset()
{
pointer = 0;
}
使用它?
像其他迭代器一样,创建一个类型为ListIterator的新对象,并将列表传递给iterate。
List< String> test = new ArrayList< String>();
test.add(Hello);
test.add(World);
test.add(Whatsapp);
ListIterator< String> iterator = new ListIterator< String>(test);
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
I am trying to create hasnext() has next() methods for a iterator so that the output of a collection will be:
"printing","elements","in","order","from","collection"
input:
[A] - ["printing", "elements", "in"]
[B] - ["order"]
[C] - ["from", "collection"]
At the moment I have my methods looking like:
public MyIterator(Collection<Collection<Object>> myColl) {
_myColl = myColl;
}
public boolean hasNext() {
if(myColl.next != null)
{
return true
}
return !queue.isEmpty();
}
public Object next() throws java.util.NoSuchElementException {
//Dont really know what to put in here....
}
Any pointers would be appreciated
Well, i don't understand your question really.. why you can't just use the normal iterator?
I will say to you how to create a basic iterator, to let you understand how things works in basic then adapt your solution:
Suppose we need to iterate over a List<T> and you want to create an helper class to do it.
class ListIterator<T>
You need two private fields
- The list to iterate
- The pointers to the last item
and 3 methods + 1 constructor
- hasNext() -> Boolean, returns true if there are more items to iterate
- next() -> Return the next element in the list
- reset() -> Reset the interal pointer
- constructor -> Just takes as argument the list to iterate
How will look the fields?
private final List<T> list; // The list where this call will iterate
private int pointer; // The pointer to the next iterable item
As said in the description, the constructor will take the reference to the list so it will just be
public ListIterator(List<T> list)
{
this.list = list;
pointer = 0;
}
save the reference and set pointer to 0 (start).
Let's talk about the methods:
hasNext
should check if our current pointer has reached the size of the list.
So it will just be (pointer != list.size())
public boolean hasNext()
{
return (pointer != list.size());
}
Will be true if more items are avaitable, false otherwise.
next
return the next item if any. Could be simplified by using our hasNext method so it will be
public T next()
{
if (!hasNext())
throw new NoSuchElementException("no field");
return list.get(pointer++);
}
Things to notice:
- T is the return because our list is type T
- list.get(pointer++) we first get the item from the list in position
pointer
then we add 1 to the pointer
The reset method is just a pointer = 0
.
public void reset()
{
pointer = 0;
}
How to use it?
Like other iterators, create a new object of type ListIterator and pass the list to iterate.
List<String> test = new ArrayList<String>();
test.add("Hello");
test.add("World");
test.add("Whatsapp");
ListIterator<String> iterator = new ListIterator<String>(test);
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
这篇关于迭代通过集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!