列挙型を反復処理するJavaプログラム
この記事では、列挙型オブジェクトを反復処理する方法を理解します。列挙型は、オブジェクトの小さなコレクションを表すデータ型です。
以下は同じのデモンストレーションです-
入力
入力が-
であると仮定しますEnum objects are defined as : red, blue, green, yellow, orange
出力
必要な出力は-
になりますPrinting the Objects: red blue green yellow orange
アルゴリズム
Step 1 – START Step 2 - Declare the objects of Enum function namely red, blue, green, yellow, orange Step 3 – Using a for loop, iterate over the objects of the enum function and print each object. Step 4- Stop
例1
enum Enum {
red, blue, green, yellow, orange;
}
public class Colour {
public static void main(String[] args) {
System.out.println("The values of Enum function are previously defined .");
System.out.println("Accessing each enum constants");
for(Enum colours : Enum.values()) {
System.out.print(colours + "\n");
}
}
} 出力
The values of Enum function are previously defined . Accessing each enum constants red blue green yellow orange
例2
曜日を印刷する例を次に示します。
import java.util.EnumSet;
enum Days {
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}
public class IterateEnum{
public static void main(String args[]) {
Days my_days[] = Days.values();
System.out.println("Values of the enum are: ");
EnumSet.allOf(Days.class).forEach(day -> System.out.println(day));
}
} 出力
Values of the enum are: Sunday Monday Tuesday Wednesday Thursday Friday Saturday
-
正方形の領域を見つけるJavaプログラム
この記事では、正方形の面積を見つける方法を理解します。正方形の面積は、次の式を使用して計算されます- side*side i.e. s2 以下は同じのデモンストレーションです- 正方形の辺がsの場合、正方形の面積はs 2で与えられます。 − 入力 入力が-であると仮定します Length of the side : 4 出力 必要な出力は-になります Area of the square : 16 アルゴリズム Step 1 - START Step 2 - Declare 2 integer values namely my_side and my_area. S
-
Javaで列挙型を拡張できますか?
いいえ 、延長できません 列挙型 Javaで。 Java列挙型は拡張できます j ava.lang.Enum クラス暗黙的に 、したがって、列挙型は別のクラスを拡張できません。 構文 public abstract class Enum> implements Comparable, Serializable { // some statements } 列挙型 列挙型 typeは、Java1.5バージョンで追加された特別なデータ型です。 。 列挙型 定数のコレクションを定義するために使用されます 、ある種の数値またはテキストデータを表さない事前定義さ