简体   繁体   English

在Java中创建列表的2D动态数组

[英]Creating a 2D dynamic array of lists in Java

I am trying to implement a 2D dynamic array of lists in Java to store a list of Course objects for a given Student and Week object. 我正在尝试在Java中实现列表的2D动态数组,以存储给定的Student和Week对象的Course对象的列表。

Something along the line, List<List<List<Course>>> listOfCoursesInWeekForStudent = new ArrayList<List<List<Course>>>(); 沿线, List<List<List<Course>>> listOfCoursesInWeekForStudent = new ArrayList<List<List<Course>>>();

Could you please show me how to create such a structure, and perform simple operations on it including add and get. 您能否告诉我如何创建这样的结构,以及如何对它执行简单的操作(包括添加和获取)。

Thank you 谢谢

Could you please show me how to create such a structure, and perform simple operations on it including add and get . 您能否告诉我如何创建这样的结构,并对它执行简单的操作,包括addget

The data-structure is List<List<List<Course>>> , so it is a List containing elements of type List<List<Course>> . 该数据结构是List<List<List<Course>>> ,所以它是一个List包含类型的元素List<List<Course>>

You create it exactly like you've shown, using: 使用以下命令完全按照显示的方式创建它:

List<List<List<Course>>> listOfListOfListOfCourses = new ArrayList<>();

Adding and getting elements works as usual, just have in mind that the elements are of type List<List<Course>> : 添加和获取元素照常工作,只需记住元素的类型为List<List<Course>>

List<List<Course>> element = ...
// Add element
listOfListOfListOfCourses.add(element);
// Get index of element
int index = listOfListOfListOfCourses.get(element);

However, as you might have noticed, the data-structure is complicated . 但是,您可能已经注意到,数据结构很复杂 I'm sure in your exact context there is a more appropriate data-structure. 我敢肯定,在您的确切上下文中,有一个更合适的数据结构。 Unfortunately you didn't provide additional information about your specific task. 不幸的是,您没有提供有关特定任务的其他信息。 You may provide it and leave a comment. 您可以提供它并发表评论。


Example

Let's try to create a simple example in order to see how such a data-structure could be of use. 让我们尝试创建一个简单的示例,以了解如何使用这种数据结构。 Therefore 因此

List<Course>             // All courses of one semester
List<List<Course>>       // All semesters of one university
List<List<List<Course>>> // All universities of one country

In this specific example you easily see that it would be of great help if you create some small wrapper classes like 在这个特定的示例中,您很容易看到,如果您创建一些小的包装器类(例如,

public class Semester {
    private List<Course> mCourses;

    public Semester(List<Course> courses) {
        this.mCourses = courses;
    }

    public List<Course> getCourses() {
        return this.mCourses;
    }
}

public class University {
    private List<Semester> mSemesters;

    public University(List<Semester> semesters) {
        this.mSemesters = semesters;
    }

    public List<Semester> getSemesters() {
        return this.mSemesters;
    }
}

Using this your data-structure breaks down to 使用此方法,您的数据结构可分解为

List<University> // Universities of one country

Far more easy to read and understand. 更容易阅读和理解。

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

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