0

So I have this ArrayList full of Strings

ArrayList<String> colours = new ArrayList<>();
    colours.add("Red");
    colours.add("Blue");

And that ArrayList is stored in another ArrayList

ArrayList<ArrayList> container = new ArrayList<>();
    container.add(colors);

And that ArrayList is stored in a HashMap

HashMap<Integer, ArrayList> map = new HashMap<>();
    map.put(1, container);

How do I access "red"? I tried

System.out.println(map.get(1).get(0).get(0));

But it gave a

Error: java: cannot find symbol
  symbol:   method get(int)
  location: class java.lang.Object

2 Answers 2

4

You should not use raw types like ArrayList<ArrayList> but use fully "cooked" types such as ArrayList<ArrayList<String>> (or even better, List<List<String>>).

Likewise, instead of HashMap<Integer, ArrayList>, use HashMap<Integer, ArrayList<ArrayList<String>>> (or even better, Map<Integer, List<List<String>>>).

If you make these changes, your map.get(1).get(0).get(0) expression will compile correctly.

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

Comments

2

Try to replace this:

  HashMap<Integer, ArrayList> map = new HashMap<>();

With:

  HashMap<Integer, List<ArrayList<String>>> map = new HashMap<Integer, List<List<String>>>();

In this case you can do this:

  System.out.println(map.get(1).get(0).get(0));

because the 1st get(1) for the map, the 2nd get(0) for the 1st List and the 3th get(0) for the 2nd List.

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.