通过迭代链表,但得到错误“的for-each并不适用于表达型”,有什么不好?

问题描述 投票:0回答:1

我目前正在执行一个线索,这是我到目前为止的代码:

1     public class DictionaryDLB{
2 
3         private Node root = new Node();
4 
5         private class Node {
6             private Character val;
7             private LinkedList<Node> next = new LinkedList<Node>();
8         }
9 
10        public void put(String key)
11        { root = put(root, key, 0); }
12
13        private Node put(Node x, String key, int d){
14            if (x == null) x = new Node();
15            if (d == key.length()) { x.val = '$'; return x; }
16            char c = key.charAt(d);
17            for(Node item : x.next){
18                if(c == item.val){
19                    item = put(item, key, d+1);
20                }
21            }
22            return x;
23        }
24    }

然而,当我尝试编译我得到私人put()方法这个错误:

DictionaryDLB.java:17: error: for-each not applicable to expression type for(Node item : (x.next)){ ^ required: array or java.lang.Iterable found: LinkedList<Node> 1 error

我抬起头,各种在线的例子,它看起来这应该工作,因为java.util.LinkedList确实实现java.lang.Iterable。但是,它不和我难倒。任何帮助或建议,将不胜感激。谢谢!

java foreach linked-list trie
1个回答
0
投票

我进口java.util.LinkedListimport java.util.*;这并不在这种情况下工作,显然。只好直接与import java.util.LinkedList;导入

© www.soinside.com 2019 - 2024. All rights reserved.