JAVA 链表如何使用 for 循环进行循环? [英] JAVA Linked List How to loop through with a for loop?
本文介绍了JAVA 链表如何使用 for 循环进行循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在尝试创建一个循环遍历链表的 for 循环.对于每条数据,它将单独列出.我正在尝试在这里学习链表,所以请不要提供数组建议.有人知道怎么做吗?
Hello I am trying to create a for loop that loops through the linked list. For each piece of data it will list it out individually. I am trying to learn linked list here, so no Array suggestions please. Anyone know how to do this?
示例输出:
- 187 号航班
- 501 航班
<小时>
我在下面的代码:
CODE I HAVE SO FAR BELOW:
public static LinkedList<String> Flights = new LinkedList<String>();
public flightinfo(){
String[] flightNum = {"187", "501"};
for (String x : flightNum)
Flights.add(x);
for (???)
}
推荐答案
只需使用增强的 for 循环,就像使用数组一样:
Just use the enhanced for-loop, the same way you'd do it with an array:
for(String flight : flights) {
// do what you like with it
}
这篇关于JAVA 链表如何使用 for 循环进行循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文