简体   繁体   中英

Java: Adding to a list of lists

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?

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

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)

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());

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM