Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 迭代数据结构和嵌套类_Java_Loops_Linked List - Fatal编程技术网

Java 迭代数据结构和嵌套类

Java 迭代数据结构和嵌套类,java,loops,linked-list,Java,Loops,Linked List,下面我有一些迭代器代码,但我很难理解它是如何工作的。特别让我困惑的是公共节点前端和语句的 Front是对静态嵌套类的引用,在for循环中我们有LinkedList.Node ptr=list.Front。该操作的左侧只是访问外部类,然后访问我们引用的静态嵌套类。该操作的右侧使用外部类的对象访问前端引用,前端引用也是静态嵌套类 那么左右两边不是一回事吗?为什么代码将一侧设置为另一侧?我知道提供的代码不完整-不是我写的。我只是想弄明白 public class LinkedList<T>

下面我有一些迭代器代码,但我很难理解它是如何工作的。特别让我困惑的是
公共节点前端
和语句的

Front是对静态嵌套类的引用,在for循环中我们有
LinkedList.Node ptr=list.Front
。该操作的左侧只是访问外部类,然后访问我们引用的静态嵌套类。该操作的右侧使用外部类的对象访问前端引用,前端引用也是静态嵌套类

那么左右两边不是一回事吗?为什么代码将一侧设置为另一侧?我知道提供的代码不完整-不是我写的。我只是想弄明白

public class LinkedList<T> {
    public static class Node<E> {
        public E data;
        public Node<E> next;
    }
    public Node<T> front;

    ...code omitted...
}


LinkedList<String> list = new LinkedList<String>();

...code omitted...

for (LinkedList.Node<String> ptr = list.front; ptr != null; ptr = ptr.next) {
    System.out.println(ptr.data);
}
公共类链接列表{
公共静态类节点{
公共电子数据;
公共节点下一步;
}
公共节点前端;
…代码省略。。。
}
LinkedList=新建LinkedList();
…代码省略。。。
for(LinkedList.Node ptr=list.front;ptr!=null;ptr=ptr.next){
系统输出打印项次(ptr数据);
}
首先,“front”不是对静态嵌套类的引用。它是对该类的“实例”的引用,这是一个重要的区别。将该类视为创建实例的模板

因此,在某个时候,有人会创建对象:

LinkedList<String> list = new LinkedList<String>();
list.front = new LinkedList.Node<String>();
list.front.data = "foo";
实际上,main方法的最后两个调用正在做相同的事情。这是因为您需要一些不同的语法来从包含类的外部创建实例。但您会注意到,在这两种情况下,您需要事先创建一个Outer的实例,而要获得一个StaticInner实例,您没有

您还将注意到,在非静态字段中,您可以访问其包含实例的私有字段“value”

您可能想看看这里:


问题中包含的代码没有使用Java迭代器。这是一个简单的for循环,用于查找链表中的下一个节点,然后打印其数据。从某种意义上说,这不是一个迭代器吗?迭代器在Java中专门定义如下:
iterator It=list.iterator()并通过诸如
hasNext()
next()
remove()
等方法对值进行迭代。这里的更多信息:好的,我明白你的意思,但回到我的问题,for语句在做什么?for语句基本上从链表的开头开始,虽然每个元素都不是空的,但打印出它的数据并移动到下一个元素。有关更详细的解释,请参见@Matt的答案。但是如果节点类是静态的,我们如何创建它的实例呢?
// Create the new node
LinkedList.Node<String> newNode = new LinkedList.Node<String>();
newNode.data = "bar";
// Since we're prepending, the existing front becomes the next for this node.
newNode.next = list.front;
// This node becomes the front of the list
list.front = newNode;
LinkedList.Node<String> ptr = list.front;
while (ptr != null) {
  // do something
  ptr = ptr.next;
}
public class Outer {
    private int value = 10;

    public static class StaticInner {

    }

    public class Inner {
        public void foo() {
            System.out.println(value);
        }
    }

    public Inner createInner() {
        return new Inner();
    }

    public static void main(String[] args) {
        Outer outer = new Outer();
        StaticInner staticInner = new StaticInner();
        Inner inner = outer.new Inner();
        Inner inner2 = outer.createInner();
    }
}