0

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.

3
  • 1
    Lists are not arrays ArrayList<HashMap<String, String>> arr = new ArrayList<HashMap<String,String>>(); arr.add(menuItems); arr.add(menuItems2); //... Commented Aug 21, 2014 at 13:33
  • Create ArrayList of ArrayLists instead of [] of ArrayLists. Commented Aug 21, 2014 at 13:35
  • The method add(HashMap<String,String>) in the type ArrayList<HashMap<String,String>> is not applicable for the arguments (ArrayList<HashMap<String,String>> - this is my next error on add Commented Aug 21, 2014 at 13:37

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

12 Comments

okay thanks i will try this. Also thanks for reference id like to also learn from my mistakes
@james Question needs to be at least 15 minute old before OP will be able to accept answer :)
great thanks very much this works perfectly ill choose this as the answer once im allowed
sorry how would i then get the menuItems back into separate ArrayList<HashMap<String, String>> types?
sorry the question still stands once ive returned this how would i then get the menuItems back. I know how to do this with an array but this is new to me
|
0
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 :)

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.