可迭代的自定义链表
iterable custom linkedlist
我正在编写一个 LinkedList class 作为作业的一部分。
LinkedList.java:
链表class:http://pastebin.com/KyKM3hP2
我正在尝试让 foreach 与我的 class 一起工作。
LinkedList<LinkedList> ListA = new LinkedList<>();
LinkedList<String> ListB = new LinkedList<>();
ListB.append("Test1");
ListB.append("Test2");
ListB.append("Test3");
ListA.append(ListB);
//this works fine
for (String i : ListB){
//do something with i
}
//this does not
for (String i : ListA.index(0)){
//do something with i
}
//both the above loops should interate over ListB
//but only the first one does
你必须将 ListA
的声明更改为这样的内容
LinkedList<LinkedList<String>> ListA = new LinkedList<>();
for (String i : ListA.index(0)){
//do something with i
}
如果你想获取ListA
中的所有字符串,那么就这样做吧。
for (List lis : YourList){
for(String s : lis){
//System.out.println(s);
}
}
如果您的 linkedList
包含 LinkedList
of LinkedLists
of Strings
那么您必须使用 2 enhanced for loops
.
如果你使用的是java8,你可以这样做。
Yourlist.forEach(templist ->{
templist.forEach(s ->{
System.out.println(s);
});
});
我正在编写一个 LinkedList class 作为作业的一部分。 LinkedList.java:
链表class:http://pastebin.com/KyKM3hP2
我正在尝试让 foreach 与我的 class 一起工作。
LinkedList<LinkedList> ListA = new LinkedList<>();
LinkedList<String> ListB = new LinkedList<>();
ListB.append("Test1");
ListB.append("Test2");
ListB.append("Test3");
ListA.append(ListB);
//this works fine
for (String i : ListB){
//do something with i
}
//this does not
for (String i : ListA.index(0)){
//do something with i
}
//both the above loops should interate over ListB
//but only the first one does
你必须将 ListA
的声明更改为这样的内容
LinkedList<LinkedList<String>> ListA = new LinkedList<>();
for (String i : ListA.index(0)){
//do something with i
}
如果你想获取ListA
中的所有字符串,那么就这样做吧。
for (List lis : YourList){
for(String s : lis){
//System.out.println(s);
}
}
如果您的 linkedList
包含 LinkedList
of LinkedLists
of Strings
那么您必须使用 2 enhanced for loops
.
如果你使用的是java8,你可以这样做。
Yourlist.forEach(templist ->{
templist.forEach(s ->{
System.out.println(s);
});
});