简体   繁体   English

Java:添加到列表列表

[英]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)); 我可以通过说groups.add(new Person(John));添加到此列表中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 . 基本上说列表中的每个项目都是一个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... 这意味着为了将一个Person添加到列表,您需要首先为要添加的元素创建一个List

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

When you want to add a Person to this inner list, you need a reference to it... 当你想添加一个Person到这个内部列表,你需要对它的引用...

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可能是更好的解决方案。

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 这是一个非常基本的示例,但是可以帮助您入门...在Collections路径中漫步可能也有帮助

actually you cannot do groups.add(new Person(John)); 实际上你不能做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; List<List<Person>> groups; you cannot do groups.add(new Person(John)); 你不能做groups.add(new Person(John)); because groups is a List<List..> and not List<Person> . 因为groupsList<List..>而不是List<Person>

What you require is get-and-add : 您需要的是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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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