ChronosChris
ChronosChris

Reputation: 7

Android:how to have an Array of type ArrayList<HashMap<String, String>>

Hey basically what I wish to do is take a number of ArrayList<HashMap<String, String>> menuItems and pass it to my onPostExecute method so to do this I intended to create an array of menuItems and pass it through...this is the code I used to do this

ArrayList<HashMap<String, String>>[] arr = new ArrayList<HashMap<String,String>>[6];
                arr[0] = menuItems;
                arr[1] = menuItems2;
                arr[2] = menuItems3;
                arr[3] = menuItems4;
                arr[4] = menuItems5;
                arr[5] = menuItems6;

then I return arr. However I am getting this error

Cannot create a generic array of ArrayList<HashMap<String,String>> 

which seems to indicate something about Java implementing its Generics at complier level which is about all I was able to find after researching questions on stack overflow and on the web in general. So my question is simply how would I do this? Either a way to solve my error or a different method of returning the menuItems.

Upvotes: 0

Views: 228

Answers (3)

Pshemo
Pshemo

Reputation: 124275

You cannot create arrays of parameterized types.

Your best choice is to use List instead of array. Also while creating references you should prefer interface over actual class.

List<List<Map<String, String>>> list = new ArrayList<List<Map<String, String>>>;
list.add(menuItems1);
list.add(menuItems2);
list.add(menuItems3);
list.add(menuItems4);
list.add(menuItems5);
list.add(menuItems6);

Upvotes: 2

Akshay Mukadam
Akshay Mukadam

Reputation: 2438

Its not possible ArrayList is itself a dynamic array part of Collection Framework refer the docs. Remove the [] bracket from your code. Quote from WikiPedia.

In computer science, a dynamic array, growable array, resizable array, dynamic table, mutable array, or array list is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard libraries in many modern mainstream programming languages.

Also refer this a very good resource fro beginners. If you want to have fixed size ArrayList answer.

for eg What I suggest. EDIT

ArrayList<Hashmap<String,String>> list =new ArrayList<Hashmap<String,String>>()
Hashmap<String,String> map=new Hasmap<String,String>()
map.put("KEY","VALUE");
list.add(map);

Remember you should each time you have to create new instance for hashmap. Because it use unique value

Upvotes: 0

An SO User
An SO User

Reputation: 25028

ArrayList<HashMap<String, String>>[] arr = new ArrayList<HashMap<String,String>>[6];  

You are trying to create an array of ArrayList. Is that on purpose or...?
ArrayLists by default are expandable. Try removing the [] and [6]

Then, use the add() method to add the data you want :)

Upvotes: 0

Related Questions