Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-将定义的整数转换为名称值_Java_Class_Defined - Fatal编程技术网

Java-将定义的整数转换为名称值

Java-将定义的整数转换为名称值,java,class,defined,Java,Class,Defined,我不知道该怎么说,但我有这样一句话: 在一个 我想要一种从值0x00000001获取字符串“CommandGroupLength”的方法 可能吗?是否要访问值为0x00000001的变量的名称?这是不可能的: public class DefinedValues { public static final int CommandGroupLength = 0x00000001; } 使用Java8,技术上至少可以通过反射获取变量的名称,请参阅 使用包含键值对的映射,可以更轻松地实现同样的目

我不知道该怎么说,但我有这样一句话:

在一个

我想要一种从值0x00000001获取字符串“CommandGroupLength”的方法


可能吗?

是否要访问值为0x00000001的变量的名称?这是不可能的:

public class DefinedValues {
  public static final int CommandGroupLength = 0x00000001;
}
使用Java8,技术上至少可以通过反射获取变量的名称,请参阅

使用包含键值对的映射,可以更轻松地实现同样的目标

Map<String,Integer> myMap= new HashMap<>();
myMap.put("CommandGroupLength", 0x00000001);
Map myMap=newhashmap();
myMap.put(“CommandGroupLength”,0x00000001);
然后编写一个函数,在Map的entrySet中搜索所有具有该值的键,因为不能确保只有一个键,所以需要返回一个集合、数组或类似的东西。 这是我的密码:

public static void main(String[] args) {
  Map<String,Integer> myMap = new HashMap<>();
  myMap.put("CommandGroupLength", 0x00000001);
  myMap.put("testA", 5);
  myMap.put("testB", 12);
  myMap.put("testC", 42);

  System.out.println("Searching for value 0x00000001 in myMap");
  Set<String> searchResults = findKeyByValue(myMap, 0x00000001);
  System.out.println("I found the following keys:");
  boolean isFirst = true;
  for(String result : searchResults) {
    if(isFirst)
      isFirst = false;
    else
      System.out.printf(", ");

    System.out.printf("%s", result);
  }
}

public static Set<String> findKeyByValue(Map<String, Integer> map, Integer value) {
  Set<String> result = new HashSet<>();

  if(value != null) {
    Set<Entry<String, Integer>> entrySet = map.entrySet();

    for(Entry<String, Integer> entry : entrySet) {
      if(value.equals(entry.getValue())) {
        result.add(entry.getKey());
      }
    }
  }

  return result;
}
publicstaticvoidmain(字符串[]args){
Map myMap=newhashmap();
myMap.put(“CommandGroupLength”,0x00000001);
myMap.put(“testA”,5);
myMap.put(“testB”,12);
myMap.put(“testC”,42);
System.out.println(“在myMap中搜索值0x00000001”);
设置searchResults=findKeyByValue(myMap,0x00000001);
System.out.println(“我找到了以下键:”);
布尔值isFirst=true;
for(字符串结果:searchResults){
如果(isFirst)
isFirst=false;
其他的
System.out.printf(“,”);
System.out.printf(“%s”,结果);
}
}
公共静态集合findKeyByValue(映射映射,整数值){
Set result=new HashSet();
if(值!=null){
Set entrySet=map.entrySet();
for(条目:entrySet){
if(value.equals(entry.getValue())){
add(entry.getKey());
}
}
}
返回结果;
}

是否要访问值为0x00000001的变量的名称?这是不可能的。但是你可以创建一个映射,它有键值对,从中你可以得到结果你为什么要这样做?可能会有一个更简单的解决方案。您可能会使用反射并遍历类的所有字段,但此时您应该停下来认真考虑是否要这样做,然后再不这样做。我们需要更多的上下文来了解您的需求,以获得一个好的解决方案。反射或常量字符串的静态数组可能会解决您的问题。@Japu_D_Cret,我接受您回答的第一句话。看来这是不可能做到的。很难解释上下文,有些东西需要它,但因为太长而无法解释。
public static void main(String[] args) {
  Map<String,Integer> myMap = new HashMap<>();
  myMap.put("CommandGroupLength", 0x00000001);
  myMap.put("testA", 5);
  myMap.put("testB", 12);
  myMap.put("testC", 42);

  System.out.println("Searching for value 0x00000001 in myMap");
  Set<String> searchResults = findKeyByValue(myMap, 0x00000001);
  System.out.println("I found the following keys:");
  boolean isFirst = true;
  for(String result : searchResults) {
    if(isFirst)
      isFirst = false;
    else
      System.out.printf(", ");

    System.out.printf("%s", result);
  }
}

public static Set<String> findKeyByValue(Map<String, Integer> map, Integer value) {
  Set<String> result = new HashSet<>();

  if(value != null) {
    Set<Entry<String, Integer>> entrySet = map.entrySet();

    for(Entry<String, Integer> entry : entrySet) {
      if(value.equals(entry.getValue())) {
        result.add(entry.getKey());
      }
    }
  }

  return result;
}