4

I' would like to store user input into an ArrayList then place that ArrayList into another ArrayList. So sort of like a main category, which contains sub-categories that contain data.

Here is some of the code:

 ArrayList<String> mainCat = new ArrayList<>();
 ArrayList<String> subCat = new ArrayList<>();

Could I add the "subCat" ArrayList in the "mainCat" ArrayList?

EugenSunic
13.8k15 gold badges68 silver badges97 bronze badges
asked Jul 28, 2015 at 23:32
6
  • 4
    Would you want the data structure to reflect the main category -> sub category hierarchy? Perhaps a Map<String, List<String>> (mapping main categories into lists of sub categories) would be a better choice then. Commented Jul 28, 2015 at 23:36
  • I agree with Mic, a map is probably a better option in that case. Commented Jul 28, 2015 at 23:39
  • @MickMnemonic why map? because of collision problem and find any arraylist type string faster? Commented Jul 28, 2015 at 23:40
  • @MickMnemonic I was trying to avoid using Map or HashMaps. But I suppose that's just me being lazy. Commented Jul 28, 2015 at 23:45
  • @KickButtowski, no, because a map is ideal for building hierarchical (tree-like) models, ones like the OP's example seems to represent. Commented Jul 28, 2015 at 23:45

4 Answers 4

6

Of course! Add it just like any other ArrayList, except the type would be ArrayList<String>.

ArrayList<ArrayList<String>> mainCat = new ArrayList<ArrayList<String>>();
ArrayList<String> subCat = new ArrayList<>();
mainCat.add(subCat);
answered Jul 28, 2015 at 23:36
Sign up to request clarification or add additional context in comments.

Comments

1

I would use a Map for this purpose, because it makes it easier to lookup sub categories based on the main category. If you use a List<List<String>>, you actually have no way to determine into which main category a given sub category belongs to. Here's an example using a TreeMap (which automatically keeps main categories in alphabetical order):

Map<String, List<String>> mainCategories = new TreeMap<>();
mainCategories.put("A", Arrays.asList("A1", "A2", "A3"));
mainCategories.put("B", Arrays.asList("B1", "B2"));
mainCategories.put("C", Arrays.asList("C1"));
System.out.println(mainCategories);
System.out.println(mainCategories.get("B"));

This prints out

{A=[A1, A2, A3], B=[B1, B2], C=[C1]}
[B1, B2]
answered Jul 29, 2015 at 0:27

Comments

1

why not ? But you need to make a slight change in the definition of mainCat:

List<List<String>> mainCat = new ArrayList<List<String>>();

Because items in mainCat collection will not be String but collections.

answered Jul 28, 2015 at 23:36

3 Comments

your list should have a type don't u think? it feels very raw ?
@KickButtowski What are you talking about? His first list uses List as a type argument, the second one uses String. Both have type arguments specified
@VinceEmigh he eidted his answer
0

Sure you can.

List<List<String>> mainCat = new ArrayList<ArrayList<String>>();
List<String> subCat = new ArrayList<String>();
mainCat.add(subCat);
answered Jul 28, 2015 at 23:36

1 Comment

if you can add good explanation, I am more than happy to vote you up

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.