Java 将 Long 转换为 Enum 类型问题

发布于 2024-12-01 16:20:18 字数 984 浏览 2 评论 0原文

我在将 Java long 类型转换为 Enum 类型时遇到了一个小问题,并且找不到解决方案。

这是我正在使用的:

public enum DataType {
    IMAGES(1),
    VIDEOS(2);

    private int value;
    private DataType(int i){
        this.value = i;
    }
}

我需要做这样的事情:

DataType dataType;
String thiz = "1";
long numb = Long.parseLong(thiz);
dataType = numb;

我得到的错误说:

将 numb 转换为 DataType 或将 dataType 转换为 long。

第二种情况:

我有这样的情况:

static String[] packetType;
String tmp=incomingData.toString(); // where incomingData is byte[]
int lastLoc = 0;
int needsSize = packetFieldSizes[tmpCurrentField-1]; // where packetFieldSizes,tmpCurrentField are integers.
thiz=tmp.substring(lastLoc, needsSize);    

packetType=thiz;  // packetType = thiz copy; where thiz is the same as used above.

我尝试将 thiz 转换为 String[] 并使用 valueOf,但是

有任何建议如何获取认为工作?

提前致谢!

I had a little problem with casting Java long type to Enum type and can't find a solution how to do that.

Here is what I'm using :

public enum DataType {
    IMAGES(1),
    VIDEOS(2);

    private int value;
    private DataType(int i){
        this.value = i;
    }
}

and I need to do something like this:

DataType dataType;
String thiz = "1";
long numb = Long.parseLong(thiz);
dataType = numb;

The error that I get says:

Convert numb to DataType or convert dataType to long.

Second Scenario:

I have this :

static String[] packetType;
String tmp=incomingData.toString(); // where incomingData is byte[]
int lastLoc = 0;
int needsSize = packetFieldSizes[tmpCurrentField-1]; // where packetFieldSizes,tmpCurrentField are integers.
thiz=tmp.substring(lastLoc, needsSize);    

packetType=thiz;  // packetType = thiz copy; where thiz is the same as used above.

I tried to convert thiz to String[] and use valueOf,but

Any suggestions how to get the thinks to work?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萌酱 2024-12-08 16:20:19

如果传递给枚举的数字是索引而不是某些任意代码(例如分辨率或行中的字符数),ordinal() 将起作用。

我使用值的访问方法、解析索引和将数字解析为枚举值的静态方法来扩展枚举。给你...

public enum DataType {
       IMAGES(1),
       VIDEOS(2);
    private int value;

    DataType(int i){
       this.value=i;
    }

    static final Map<Integer,DataType> inverseIndex;
    static {
        inverseIndex = new HashMap<Integer,DataType> ();
        for (DataType dt:DataType.values()) {
            inverseIndex.put(dt.getValue(), dt);
        }   
    }

    public int getValue() {
        return value;
    }

    public static DataType resolve(int number) {
        return inverseIndex.get(number);
    }
}

请注意,此解决方案将不起作用,因为您的地图枚举值不是双射的,因此您的枚举类型中的枚举可能只有不同的值。

ordinal() will work if the numbers you are passing to the enum are indexes and not some arbitrary code, like resolution or number of chars in a line.

I'd extends the enum with an accesor method for the value, a resolution index and a static method that resolves a number into a Enum value. Here you go...

public enum DataType {
       IMAGES(1),
       VIDEOS(2);
    private int value;

    DataType(int i){
       this.value=i;
    }

    static final Map<Integer,DataType> inverseIndex;
    static {
        inverseIndex = new HashMap<Integer,DataType> ();
        for (DataType dt:DataType.values()) {
            inverseIndex.put(dt.getValue(), dt);
        }   
    }

    public int getValue() {
        return value;
    }

    public static DataType resolve(int number) {
        return inverseIndex.get(number);
    }
}

Note that this solution won't work is your map Enum-value is not bijective, so you may only have distinct values for the enums in your enumType.

飘落散花 2024-12-08 16:20:18

Enum 已经为其每个实例提供了一个唯一的整数。查看 ordinal ()。 (请注意,它是从零开始的。)

如果您需要从 long 转换为 DataType,您可以执行

DataType dataType;
String thiz;
long numb = Long.parseLong(thiz);
dataType = DataType.values()[(int) numb];

枚举常量、字符串之间的转换的完整列表整数可以在这个答案中找到:

Enum already provides a unique integer for each of it's instances. Check out ordinal(). (Note that it's zero-based though.)

If you need to go from a long to a DataType you can do

DataType dataType;
String thiz;
long numb = Long.parseLong(thiz);
dataType = DataType.values()[(int) numb];

A complete list of conversions from and to enum constants, strings and integers can be found in this answer:

水溶 2024-12-08 16:20:18

如果由于某种原因您需要自己分配数字,从而无法使用 aioobe 的良好解决方案,您可以执行如下操作:

public enum DataType {
    IMAGES(1),
    VIDEOS(2);

 private final int value;
 private DataType(int i){
    this.value=i;
 }
 public static DataType getByValue(int i) {
     for(DataType dt : DataType.values()) {
         if(dt.value == i) {
             return dt;
         }
     }
     throw new IllegalArgumentException("no datatype with " + i + " exists");
 }

静态方法 getByValue() 搜索 DataType< /code> 与提供的号码。

If for some reason you need to assign the numbers yourself and thereby can't use aioobe's good solution, you can do something like the following:

public enum DataType {
    IMAGES(1),
    VIDEOS(2);

 private final int value;
 private DataType(int i){
    this.value=i;
 }
 public static DataType getByValue(int i) {
     for(DataType dt : DataType.values()) {
         if(dt.value == i) {
             return dt;
         }
     }
     throw new IllegalArgumentException("no datatype with " + i + " exists");
 }

The static method getByValue() searches for the DataType with the provided number.

青衫负雪 2024-12-08 16:20:18

除了@aioobe的答案之外,您还可以滚动您自己的 getInstance 方法。这将提供更大的灵活性,因为您不会依赖于序数。

public enum DataType {
    .
    .
    public static final DataType getInstance(final int i){
        for(DataType dt: DataType.values()){
            if(dt.value == i){
                return dt;
            }
        }

        return null;
    }
}

In addition to @aioobe's answer, you could roll your own getInstance method. This would provide more flexibility, since you wouldn't be dependent on the ordinal.

public enum DataType {
    .
    .
    public static final DataType getInstance(final int i){
        for(DataType dt: DataType.values()){
            if(dt.value == i){
                return dt;
            }
        }

        return null;
    }
}
掌心的温暖 2024-12-08 16:20:18

aioobe 的正确答案。也许还有其他一些担忧?

对于枚举索引,最好使用 int 而不是 long。
它可能是这样的:

String indexAsString;
int index = Integer.parseInt(indexAsString)-1;
DataType dataType = DataType.values()[index];

请注意“-1”,因为数组是从零开始的,而索引是从一开始的。

Correct answer from aioobe. Maybe some other concerns ?

You could be better of using int instead of long, for the enum index.
It could be like :

String indexAsString;
int index = Integer.parseInt(indexAsString)-1;
DataType dataType = DataType.values()[index];

Please note the "-1", as arrays are zero-based while your index is one-based.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文