1

I am trying to create an array of HashMaps within a class and then retrieve a hashmap from the array for calculation purposes. Here is my code:

HashMap<String, Integer>[] boardPopulation= (HashMap<String, Integer>[]) new HashMap[populationSize];
    for(int i=0; i < populationSize; i++){
        generateQueens();
        boardPopulation[i] = queenMap;
    }
    for(int i=0; i < populationSize; i++){
        queenMap = boardPopulation[i];
        printBoard();
    }

When I compile I get two issues:

  1. Board.java uses unchecked or unsafe operations, recompile with -Xlint:unchecked.
  2. When I compile with "Xlint:unchecked" I get

Warning: [unchecked] unchecked cast HashMap[] boardPop.......(same as line 1 of above code)

required: HashMap[] found: HashMap[]

Please help me ! :D

Ideally I would like to not have to use Xlint unchecked, but ultimately I really just need to be able to retrive a HashMap from the list and assign it to queenMap so I can do calculations within the class.

Thankyou

3

1 Answer 1

2

Sorry for answering a slightly different question but if an array isn't essential, have you tried using a generic list:

List<Map<String, Integer>> boardPopulations = new ArrayList<>();
boardPopulations.add(new HashMap<>()); 
Sign up to request clarification or add additional context in comments.

4 Comments

I literally just found something very similar and I think it is doing what I need now. Code: < List<Map<String, Integer>> boardPopulation = new ArrayList<Map<String, Integer>>(); for(int i=0; i < populationSize; i++){ generateQueens(); boardPopulation.add(i, queenMap);
Cool, looks better. If you're using java 7 I think should be able to just do List<Map<String, Integer>> boardPopulation = new ArrayList<>(), the type of the ArrayList<T> constructor should be inferred from the declaration of boardPopulation as a List<Map<String, Integer>>
Using Java 8. I now have a new issue. Is there any chance I could ask you to private chat to go into more detail? I'm asking because the new issue is less related to this question and more related to understanding the problem I am solving.
Go ahead, I don't know how to start one but I assume you do

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.