简体   繁体   English

Java中可扩展的列表列表

[英]Extensible list of lists in Java

I have a GUI application which lets user to create sections and subsections for a specific section. 我有一个GUI应用程序,允许用户为特定部分创建部分和子部分。 The user can add new sections and add new subsections to a section any time. 用户可以随时添加新的部分并向部分添加新的子部分。 Finally I have a list viewer which will display the whole things every time I click a 'Refresh' button. 最后我有一个列表查看器,每次单击“刷新”按钮时都会显示整个内容。

For the section part, I have created a List of string: 对于部分部分,我创建了一个字符串列表:

public static ArrayList<String> sections = new ArrayList<String>();

For the subsection part I want to create a List of List which will create a List of all the sections. 对于子部分,我想创建一个List列表,它将创建所有部分的List。 I have used something like this, but the problem is I cannot uniquely identify each List using a section name: 我使用过这样的东西,但问题是我无法使用节名称唯一地标识每个List:

public static ArrayList<ArrayList<String>> subsections = new ArrayList<ArrayList<String>>();

I want a data structure which will let me create a list based on a section name and will also let me add string in it any time. 我想要一个数据结构,它允许我根据部分名称创建一个列表,并且还允许我随时添加字符串。 Any help? 有帮助吗?

你需要Map ,因为它是一个键值对的集合。

public static Map<String,List<String>> subsections = new HashMap<String,List<String>>();

使用HashMap

Map<String, List<String>> section = new HashMap<String, List<String>>();

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

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