简体   繁体   English

如何将ArrayList转换为数组数组?

[英]How do you convert an ArrayList to an array of arrays?

I'm doing my 2nd Year University software engineering project and have hit a problem. 我正在做我的第二年大学软件工程项目,遇到了一个问题。 I am fairly new to the Java language (and programming in general) so there is a chance I'm just not understanding the syntax well. 我对Java语言(以及一般而言的编程语言)还很陌生,所以有可能我只是不太了解语法。

I have a method that I want to be used to return an array of arrays of type Object. 我有一个方法要用于返回Object类型的数组的数组。 This is to be used in a swing JTable. 这将在Swing JTable中使用。 Therefore each elements of the array (which are arrays themselves) are the rows in the table and the elements of the 'inner' arrays are the value of each column in that row. 因此,数组的每个元素(本身就是数组)是表中的行,“内部”数组的元素是该行中每一列的值。

I do not know how many columns there will be while coding and therefore I have made a method that generates this array of arrays depended on other objects. 我不知道编码时将有多少列,因此我做了一个方法来生成依赖于其他对象的数组数组。

This is my code: 这是我的代码:

    public Object[][] getData(ArrayList<IModule> modules) {

    ArrayList<String[]> data = new ArrayList();

    for (Iterator<IModule> m = modules.iterator() ; m.hasNext(); ) {
        IModule module = m.next();
        for (Iterator<ICoursework> c = module.getCoursework().iterator() ; c.hasNext() ; ) {
            ICoursework coursework = c.next();
            String[] row = new String[6];

            row[0] = module.getModuleTitle();
            row[1] = module.getModuleCode();
            row[2] = coursework.getTitle();
            row[3] = coursework.dateToString();
            row[4] = coursework.getLecturer().getFirstName() 
                    + coursework.getLecturer().getSecondName();
            row[5] = getSubmissionDetails(coursework, row);

            data.add(row);
        }
    }
    data.trimToSize();

    return data.toArray();
}

This obviously does not compile as the ArrayList.toArray() method returns a Object[] where I need it to return a Object[][]. 这显然不会编译,因为ArrayList.toArray()方法返回Object [],而我需要它来返回Object [] []。

As I understand it since I had an ArrayList of arrays this should be converted to an array that then contained arrays. 据我了解,由于我有一个ArrayList数组,因此应将其转换为包含数组的数组。 But it does not. 但事实并非如此。

If you could post an answer that would be helpful. 如果您可以发布答案,那将有所帮助。 Thank you. 谢谢。

尝试return data.toArray(new String[][]);

You can't do that way. 你不能那样做。 Try: 尝试:

public Object[][] getData(ArrayList<IModule> modules) {

    ArrayList<String[]> data = new ArrayList<String[]>();

    for (Iterator<IModule> m = modules.iterator() ; m.hasNext(); ) {
        IModule module = m.next();
        for (Iterator<ICoursework> c = module.getCoursework().iterator() ; c.hasNext() ; ) {
            ICoursework coursework = c.next();
            String[] row = new String[6];

            row[0] = module.getModuleTitle();
            row[1] = module.getModuleCode();
            row[2] = coursework.getTitle();
            row[3] = coursework.dateToString();
            row[4] = coursework.getLecturer().getFirstName() 
                    + coursework.getLecturer().getSecondName();
            row[5] = getSubmissionDetails(coursework, row);

            data.add(row);
        }
    }

    Object[][] arr = new Object[data.size()][6];

    for (int i = 0; i < data.size(); i++) {
        String[] strings = data.get(i);
        for (int j = 0; j < strings.length; j++) {
            arr[i][j] = strings[j];
        }
    }

    return arr;
}

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

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