Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.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 使用字符串变量引用ArrayList_Java - Fatal编程技术网

Java 使用字符串变量引用ArrayList

Java 使用字符串变量引用ArrayList,java,Java,是否可以使用字符串引用数组 for(Entry<String, GameInformation> gameSet : GameInfoPuller.gameMap.entrySet()) { //For each entry put the games key into key and the value into value. String key = gameSet.getKey(); GameInformation value = gameS

是否可以使用字符串引用数组

for(Entry<String, GameInformation> gameSet : GameInfoPuller.gameMap.entrySet())

  {
     //For each entry put the games key into key and the value into value.
     String key = gameSet.getKey();
     GameInformation value = gameSet.getValue();

     String genreSelect = genreSelect(value);

     genreSelect.add(Item here); <---- is there a way to do this?
  } 


private static String genreSelect(GameInformation value)
{
  String genreValue = "";

//Check each entry for the value of its genre and for each genre put into the corresponding category.

  if(value.getGameGenre().equalsIgnoreCase("Fantasy"))
  {
     genreValue = "fantasy";
  }
  else if(value.getGameGenre().equalsIgnoreCase("Historical"))
  {
      genreValue = "historical";
  }
  else if(value.getGameGenre().equalsIgnoreCase("Sci-Fi"))
  {
        genreValue = "sciFi";
  }
  else if(value.getGameGenre().equalsIgnoreCase("Horror"))
  {
        genreValue = "horror";
  }
  else if(value.getGameGenre().equalsIgnoreCase("Sports"))
  {
        genreValue = "sports";
  }
  else if(value.getGameGenre().equalsIgnoreCase("Real Life"))
  {
        genreValue = "realLife";
  }
  else if(value.getGameGenre().equalsIgnoreCase("Super-Hero"))
  {
        genreValue = "superHero";
  }          
  else
  {
        System.out.println("Game: " + " does not have a genre.");
  }
return genreValue;
}
for(条目游戏集:GameInfoPuller.gameMap.entrySet())
{
//对于每个条目,将游戏键放入键中,将值放入值中。
String key=gameSet.getKey();
GameInformation值=gameSet.getValue();
字符串genreSelect=genreSelect(值);

genreSelect.在此处添加(项目);不,这是不可能的。
您可以将您的流派存储为
列表的第一项,然后根据需要向该列表添加更多的项。

否,“字符串”不能作为对“ArrayList”的引用,因为它们是不兼容的类型

但是,您可以使用一些技术将字符串映射到“ArrayList”实例。例如:

  // Initialization of Map Collection.
  Map<String, ArrayList> stringToArrayListMap = new HashMap<>();
  stringToArrayListMap.put("key for the first array list", new ArrayList());
//地图集合的初始化。
Map stringToArrayListMap=新HashMap();
stringToArrayListMap.put(“第一个数组列表的键”,new ArrayList());

你也可以使用反射,但我不想讨论这个问题,因为这是一个黑客行为,需要反射可能是一种设计气味的表现。

这看起来像是一个有缺陷的设计。但是,你可以使用映射来实现这一点。我能问一下为什么要在示例中添加java注释吗?如果这是stackoverflow中的某个标准,为什么要使用这个promoted?描述1:1的源代码注释(如上所述)代码所做的是一种附加责任,不会增加价值。如果第一条语句需要注释,那么这与第二条语句有什么不同?为什么不需要注释呢?当然,我不是建议在那里添加注释,但是在代码中,一致性(包括注释的使用)是基本的,所以我想知道为什么它没有坚持。谢谢!