问题
I am having trouble printing out individual Strings from Interable object. I have this given function prefixMatch(String someword) that returns Iterable (that keeps a LinkedList of Strings, I think). I have tried turning it into a list but it wont work. Dose anyone know how to get the Strings out of it one by one?
tst is a Ternary search tree
Iterable<String> words = tst.prefixMatch(word);
回答1:
If it's Iterable you can do an extended for on it.
Iterable<String> iterable;
for(String s : iterable){
//Do whatever you want
}
Resources:
- Oracle.com - The foreach loop
Related topics:
- What is the Iterable interface used for?
回答2:
for (String s: words) {
System.out.println(s);
}
回答3:
The java 8 way, using forEach
:
words.forEach(word-> System.out.println(word));
回答4:
I think this will help you, here i am using Iterable.
TreeSet treeSet = new TreeSet<String>();
treeSet.add("A");
treeSet.add("B");
Iterable<String> it = treeSet;
Iterator iterator = it.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
来源:https://stackoverflow.com/questions/9947221/how-to-print-out-individual-strings-from-iterablestring