0

Consider the following declaration...

List<List<Person>> groups;

I can add to this list by saying groups.add(new Person(John));

Is there any way I can add to the inner list rather than the outer one?

6
  • Create a List, add a person to it, add it to the other List. Commented Oct 16, 2013 at 2:35
  • Can I do this without creating another list? I just want to be able to say groups. etc etc and add either a new list of people or add more people to an existing list of people. Commented Oct 16, 2013 at 2:38
  • You have a List of List. You need to create a new List and do groups.add(theNewList). You can then add to it directly theNewList.add(new Person()) or by getting a reference from the outer List as groups.get(0).add(new Person()) Commented Oct 16, 2013 at 2:40
  • 1
    @PM The add method returns a boolean. You could create an anonymous inner class that adds the element. For a one liner groups.add((new ArrayList<Person>() {{add(new Person("John");}})); but that's a really bad idea. Commented Oct 16, 2013 at 2:47
  • 1
    Try groups.add(new ArrayList<Person>(Arrays.asList(new Person("John")))); Commented Oct 16, 2013 at 3:12

4 Answers 4

6

List<List<Person>> groups; basically says that each item in the list is a list of Person.

This means that in order to add a Person to the list, you need to first create a List for the element you want to add...

List<Person> person = //...
groups.add(person);

When you want to add a Person to this inner list, you need a reference to it...

Person aPerson = //...
groups.get(0).add(aPerson);

For example...

Updated based on comments

A Map might be a better solution for "grouping" like items, for example...

Map<String, List<Person>> groups = new HashMap<>();
List<Person> persons = groups.get("family");
if (persons == null) {
    persons = new ArrayList<>(25);
    groups.put("family", persons);
}
persons.add(aPerson);

This is a VERY basic example, but help you getting started...A walk through the Collections trail might also help

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

9 Comments

Kind of off topic here but just need a quick answer. How do i override that get method? Because Im actually storing a list of people and every group of people has a name so ild like to be able to say groups.get("friends").add("joe");
Have you considered using a map of lists rather than a list of lists? Maps have a get(Object) method that you can use like this.
@Jules that sounds like what I need. I'll have to read into Maps though, I've never used them.
@Clay I was wondering about that how you would identify a group
@Clay I added a VERY basic example of using a Map which might help
|
0

actually you cannot do groups.add(new Person(John));

you can do:

ArrayList<Person> t = new ArrayList<Person>();
t.add(new Person());
groups.add(t)

Comments

0

With List<List<Person>> groups; you cannot do groups.add(new Person(John)); because groups is a List<List..> and not List<Person>.

What you require is get-and-add:

List<List<Person>> groups;
//add to group-1
groups = new ArrayList<List<Person>>();
//add a person to group-1
groups.get(0).add(new Person());

//Or alternatively manage groups as Map so IMHO group fetching would be more explicit 
Map<String, List<Person>> groups;
//create new group => students, 
groups = new HashMap<String, List<Person>>();//can always use numbers though
groups.put("students", new ArrayList<Person>());

//add to students group
groups.get("students").add(new Person());

Comments

0

You could define a generic double list class to do it for you. Obviously you can extend this if you have certain business logic that will help you figure out internally which list to add to (without giving the index).

import java.util.*;

public class DoubleList<T>
{
    private List<List<T>> list;

    public DoubleList()
    {
        list = new ArrayList<List<T>>();
    }

    public int getOuterCount()
    {
        return list.size();
    }

    public int getInnerCount(int index)
    {
        if (index < list.size())
        {
            return list.get(index).size();
        }
        return -1;
    }

    public T get(int index1, int index2)
    {
        return list.get(index1).get(index2);
    }

    public void add(int index, T item)
    {
        while (list.size() <= index)
        {
            list.add(new ArrayList<T>());
        }
        list.get(index).add(item);
    }

    public void add(T item)
    {
        list.add(new ArrayList<T>());
        this.add(list.size() - 1, item);
    }
}

Then you use it like this:

DoubleList<String> mystrs = new DoubleList<String>();

mystrs.add("Volvo");
mystrs.add(0, "Ferrari");

mystrs.add(1, "blue");
mystrs.add(1, "green");

mystrs.add(3, "chocolate");

for (int i = 0; i < mystrs.getOuterCount(); i++)
{
    System.out.println("START");
    for (int j = 0; j < mystrs.getInnerCount(i); j++)
    {
        System.out.println(mystrs.get(i,j));
    }
    System.out.println("FINISH");
}

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.