Java - ListIterator和hasNext [英] Java - ListIterator and hasNext

查看:209
本文介绍了Java - ListIterator和hasNext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Java,我遇到了 ListIterator 的问题。我有一个包含这些字符的列表:b o o n g o o n n o。我的代码返回buongiorno,而我期望它打印buongiorn,没有尾随的o。因为 hasNext()函数,我期待这一点。我的代码使用递归。你能解释一下原因吗?

I'm studying Java, and I've a problem with ListIterator. I've a List with these characters: b u o n g i o r n o. My code returns "buongiorno", while I was expecting it to print "buongiorn", without the trailing "o". I was expecting this because of the hasNext() function. My code uses recursion. Can you explain to me the reason?

public static String creaStringa(List<Character> lista) {
    System.out.println(lista.size());
    ListIterator<Character> it = lista.listIterator();
    return ricCrea(it);
}


public static String ricCrea(ListIterator<Character> it) {
    if(!(it.hasNext())) {
        return "";
    else
        return String.valueOf(it.next()) +ricCrea(it);
}


推荐答案

如果更清楚列表只有一个元素,让我们说b。 hasNext()实际上会返回true, next()会读取它,迭代将在此之后结束。

It would be more clear if the list had only one element, lets say "b". hasNext() would actually return true, and next() would read it and the iteration would end after that.

说明:

如果您致电 Iterator< Object> ;任何非空列表上的it = list.iterator()(即使它只有一个元素),你得到 true 来调用 hasNext()。这是因为迭代器在第一个元素之前被初始化:

If you call Iterator<Object> it= list.iterator() on any non-empty list (even if it has only one element), you get true for calling the hasNext(). That is because the iterator is initialized BEFORE the first element:

  b u n g i o r n o
 ^
 i - iterator

当你拨打 next()时它会做两次事情:

And when you call next() it does two things:


  • 它读取迭代器前面的元素,

  • 移动迭代器之后刚读过的元素,在下一个元素之前。

在你的例子中 - 它打印b并在u之前停止:

In your example - it prints "b" and stops before the "u":

  b u n g i o r n o 
   ^
   i

在结束前:

  b u n g i o r n o
                 ^
                 i

它实际上有下一个值 - o。调用 next()将读取该值并跳转到 o 之后。没有更多的元素。 hasNext()将显示false,并且调用 next()将导致异常。

It actually has the next value - "o". Calling the next() will read that value and jump after the o. There are no more elements. hasNext() will show false, and calling next() will result in an exception.

技术细节:

基本思路如何实现迭代器:
- 当 Iterator 是通过在列表 iterator()创建的>,其内部变量名为 next 指向列表的第一个元素。
- hasNext()只检查 next 是否!= null
- next()返回 next 并设置 next 显示下一个元素。

Basic idea how iterator is implemented is this: - when the Iterator is created by calling the iterator() on a List, its inner variable called next is pointing to the first element of the list. - hasNext() just checks whether the next is != null. - next() returns next and sets the next to show the next element.

这是 java.util.ArrayList Iterator(省略了一些细节) ):

This is java.util.ArrayList Iterator (with some details omitted):

public Iterator<E> iterator() {
     return new Itr();
}

private class Itr implements Iterator<E> {
     int cursor;       // index of next element to return
     int lastRet = -1; // index of last element returned; -1 if no such
     int expectedModCount = modCount;

     public boolean hasNext() {
         return cursor != size;
     }

     public E next() {
         checkForComodification();
         int i = cursor;
         Object[] elementData = ArrayList.this.elementData;
         cursor = i + 1;
         return (E) elementData[lastRet = i];
     }

 }

这篇关于Java - ListIterator和hasNext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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