[英]iterable custom linkedlist
i am writing a LinkedList class as part of an assignment. 我正在编写LinkedList类作为赋值的一部分。 LinkedList.java:
LinkedList.java:
LinkedList class: http://pastebin.com/KyKM3hP2 LinkedList类: http : //pastebin.com/KyKM3hP2
i am trying to get foreach working with my 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
you have to change the declaration of ListA
to something like this 你必须将
ListA
的声明ListA
为这样的东西
LinkedList<LinkedList<String>> ListA = new LinkedList<>();
for (String i : ListA.index(0)){
//do something with i
}
if you want to get all strings in ListA
then do it this way. 如果你想获得
ListA
所有字符串,那么就这样做。
for (List lis : YourList){
for(String s : lis){
//System.out.println(s);
}
}
if your linkedList
contains LinkedList
of LinkedLists
of Strings
then you have to use 2 enhanced for loops
. 如果您的
linkedList
包含Strings
LinkedList
的LinkedLists
,那么您必须使用2个增强的for loops
。
if you are using java 8 you can do it like this. 如果你使用的是java 8,你可以这样做。
Yourlist.forEach(templist ->{
templist.forEach(s ->{
System.out.println(s);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.