Java cast to enum type problem

There were some problems converting Java long type to enum type, and a solution could not be found

This is what I'm using:

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

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

I need to do something like this:

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

The mistake I got said:

The 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 on how to make ideological work?

Thank you in advance!

Solution

Enumeration has provided a unique integer for each instance Check the order () (note that it is based on zero.)

If you need to go from long to datatype, you can do so

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

In this answer, you can find the complete conversion list of enumeration constants, strings and integers:

> Conveniently map between enum and int / String

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>