The Enum class is the common base class of all Java language enumeration types.
Let us see an example to iterate over enum values using for loop −
public class Demo { public enum Vehicle { CAR, BUS, BIKE } public static void main(String[] args) { for (Vehicle v : Vehicle.values()) System.out.println(v); } }
CAR BUS BIKE
現在讓我們看另一個例子,使用for each循環迭代枚舉值:
import java.util.stream.Stream; public class Demo { public enum Work { TABLE, CHAIR, NOTEPAD, PEN, LAPTOP } public static void main(String[] args) { Stream.of(Work.values()).forEach(System.out::println); } }
TABLE CHAIR NOTEPAD PEN LAPTOP
以上是在Java中迭代枚舉值的詳細內容。更多資訊請關注PHP中文網其他相關文章!