The List interface extends Collection interface and stores a sequence of elements. The List interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list. Unlike allows duplicate elements, allows multiple null values if null value is allowed in the list.
Java List provides two kinds of iterators using iterator() or listIterator(). First kinds of iterators using iterator() or listIterator()。Y (
Get iterator from the list to iterate through its elements.Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()) { System.out.print(iterator.next() + " "); }
Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()) { System.out.print(iterator.next() + " "); }
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5)); Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()) { System.out.print(iterator.next() + " "); } } }
1 2 3 4 5
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5)); Iterator<Integer> iterator = list.listIterator(); while(iterator.hasNext()) { System.out.print(iterator.next() + " "); } } }
1 2 3 4 5
以上是如何在Java中迭代一個列表?的詳細內容。更多資訊請關注PHP中文網其他相關文章!