Java で Enum を Int に変換する
このチュートリアルでは、Java で Enum を Int に変換する方法を示します。
Java で Enum を Int に変換する
Java には、enum を int に、またはその逆に変換する組み込み機能はありませんが、これらの操作を実行するメソッドを作成できます。 value()
メソッドを使用して、int 値を持つ enum を int に変換できます。
段階的なプロセスを見てみましょう。
-
まず、int 値で Enum を生成します。 私たちの場合には:
enum Cars { Toyota(10), Mercedes(20), Tesla(30), BMW(40), Audi(50); }
-
enum
Cars
値を取得するメソッドを作成します。public int getCarAsInt() { return cars; }
-
Cars
列挙メンバーを int に変換するメソッドを作成します。 このメソッドには、for
ループと、整数値を返すif
条件ステートメントが含まれます。public static int convertCarToInt(Cars inputCar) { for (Cars cars : Cars.values()) { if (cars.getCarAsInt() == inputCar.getCarAsInt()) { return cars.getCarAsInt(); } } return -1; }
-
int を enum
Cars
に変換する同様のメソッドを作成することもできます。public static Cars convertIntToCar(int intCars) { for (Cars cars : Cars.values()) { if (cars.getCarAsInt() == intCars) { return cars; } } return null; }
-
最後に、上記のメソッドを各列挙型メンバーに適用します。 例を試して、各メソッドを enum
Cars
の各メンバーに適用してみましょう。これにより、enum メンバーが int に変換され、int が enum メンバーに戻されます。package delftstack; enum Cars { Toyota(10), Mercedes(20), Tesla(30), BMW(40), Audi(50); private final int cars; Cars(int cars) { this.cars = cars; } public int getCarAsInt() { return cars; } public static int convertCarToInt(Cars inputCar) { for (Cars cars : Cars.values()) { if (cars.getCarAsInt() == inputCar.getCarAsInt()) { return cars.getCarAsInt(); } } return -1; } public static Cars convertIntToCar(int intCars) { for (Cars cars : Cars.values()) { if (cars.getCarAsInt() == intCars) { return cars; } } return null; } } public class Example { public static void main(String[] args) { // For enum member Toyota Cars Toyota_Car = Cars.Toyota; int intCars = Toyota_Car.getCarAsInt(); // Get car as integer value System.out.println("Get Toyota car as int value :" + intCars); Toyota_Car = Cars.convertIntToCar(10); // Convert integer value to corresponding car value System.out.println("Convert integer 10 to car enum :" + Toyota_Car); intCars = Cars.convertCarToInt(Cars.Toyota); // Convert Cars value to corresponding integer value System.out.println("Convert Toyota car to integer :" + intCars + "\n"); // For enum member Mercedes Cars Mercedes_Car = Cars.Mercedes; intCars = Mercedes_Car.getCarAsInt(); // Get car as integer value System.out.println("Get Mercedes car as int value :" + intCars); Mercedes_Car = Cars.convertIntToCar(20); // Convert integer value to corresponding car value System.out.println("Convert integer 10 to car enum :" + Mercedes_Car); intCars = Cars.convertCarToInt(Cars.Mercedes); // Convert Cars value to corresponding integer value System.out.println("Convert Mercedes car to integer :" + intCars + "\n"); // For enum member Tesla Cars Tesla_Car = Cars.Tesla; intCars = Tesla_Car.getCarAsInt(); // Get car as integer value System.out.println("Get Tesla car as int value :" + intCars); Tesla_Car = Cars.convertIntToCar(30); // Convert integer value to corresponding car value System.out.println("Convert integer 10 to car enum :" + Tesla_Car); intCars = Cars.convertCarToInt(Cars.Tesla); // Convert Cars value to corresponding integer value System.out.println("Convert Tesla car to integer :" + intCars + "\n"); // For enum member BMW Cars BMW_Car = Cars.BMW; intCars = BMW_Car.getCarAsInt(); // Get car as integer value System.out.println("Get BMW car as int value :" + intCars); BMW_Car = Cars.convertIntToCar(40); // Convert integer value to corresponding car value System.out.println("Convert integer 10 to car enum :" + BMW_Car); intCars = Cars.convertCarToInt(Cars.BMW); // Convert Cars value to corresponding integer value System.out.println("Convert BMW car to integer :" + intCars + "\n"); // For enum member Audi Cars Audi_Car = Cars.Audi; intCars = Audi_Car.getCarAsInt(); // Get car as integer value System.out.println("Get Audi car as int value :" + intCars); Audi_Car = Cars.convertIntToCar(50); // Convert integer value to corresponding car value System.out.println("Convert integer 10 to car enum :" + Audi_Car); intCars = Cars.convertCarToInt(Cars.Audi); // Convert Cars value to corresponding integer value System.out.println("Convert Audi car to integer :" + intCars + "\n"); } }
上記のコードは、各 enum
Cars
メンバーを int に、またはその逆に変換します。 出力を参照してください。Get Toyota car as int value :10 Convert integer 10 to car enum :Toyota Convert Toyota car to integer :10 Get Mercedes car as int value :20 Convert integer 10 to car enum :Mercedes Convert Mercedes car to integer :20 Get Tesla car as int value :30 Convert integer 10 to car enum :Tesla Convert Tesla car to integer :30 Get BMW car as int value :40 Convert integer 10 to car enum :BMW Convert BMW car to integer :40 Get Audi car as int value :50 Convert integer 10 to car enum :Audi Convert Audi car to integer :50
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook