0

Is it possible to use a String to reference an array?

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;
}

Thanks for the help!

1
  • This looks like a really flawed design. You can, however, use a Map<String, ArrayList> to do this. Commented May 5, 2014 at 18:34

2 Answers 2

1

No, a "String" cannot be a reference to an "ArrayList" because they are incompatible types.

You can, however, use some techniques to map a string to an "ArrayList" instance. For example:

  // Initialization of Map Collection.
  Map<String, ArrayList> stringToArrayListMap = new HashMap<>();
  stringToArrayListMap.put("key for the first array list", new ArrayList());

You may also be able to use reflection, but I wont get into that since that is a hack and requiring it probably is an indication of a design smell.

Sign up to request clarification or add additional context in comments.

1 Comment

Can I ask why a java comment was added to the example? If this is some standard in stackoverflow, why is this promoted? Source code comments which describe 1:1 (as is the first statement above) what the code is doing, is an added liability and does not add value. If a comment was needed for the first statement, how is that any different from the second statement--why wouldnt one be needed there also? Of course I am not suggesting one be added there, though, but in code, consistency (including use of comments) is fundamental, so I am wondering why it isnt adhered consistently. Thanks!
0

No, it's not possible.
You could store your genre as the first item of a List<String> and than add further items to that list as needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.