HOWTO · Java
Java에서 Enum을 Int로 변환
이 튜토리얼은 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); } -
열거형
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; } -
마지막으로 위의 방법을 각 열거형 멤버에 적용합니다. 예를 들어 열거형
Cars의 각 구성원에 각 메서드를 적용하여 열거형 구성원을 int로 변환하고 int를 다시 열거형 구성원으로 변환해 보겠습니다.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