Reputation: 183
Hello I would like to implement a Hash map that maps a specific Date to an array of ints. the size of the array is 32
I tried this and it compiles :
HashMap<Date,int[]> coord_map = new HashMap<Date, int[]>();
but I am not sure how this works since I did not give a size for the array of integers.
also I tired this:
int[] arr = new int[32];
for(int i =0; i <32; i++){
arr[i] = 0; // initialize the array to 0.
}
HashMap<Date, arr> attraction_date = new HashMap<Date, arr>();
this gives me a compiler error "Cannot find a class or type named arr"
thank you
I now have a followup question:
I am successfully using one hash map that takes in a date and maps it to an int array.
now I want to use multiple instances of this hashmap. since my project deals with theme park data, there will be one hashmap for every attraction.
so how would I implement an array list of hash maps. To keep track of 20 attractions?
If anyone can show me a sample setup code, that initializes everything, that would be helpful.
thank you again,
Upvotes: 1
Views: 342
Reputation: 37823
What you have on top is good, you don't need to specify a size. This will work
Map<Date, int[]> coordMap = new HashMap<Date, int[]>();
coordMap.put(aDate, new int[]{1,2});
coordMap.put(anotherDate, new int[]{3,4,5,6});
...
So each value int[]
can have a different size.
Upvotes: 3
Reputation: 70989
It depends on what you do. If the problem requires that you have an array of exactly 32 ints then create a wrapper class IntArray32 and use HashMap<Date, IntArray32>
. Otherwise what you have written will work, it will simply allow you to have int array of any size as value.
Upvotes: 4