简体   繁体   English

Java 添加到 ArrayList<String> 然后转换为 Object[][]

[英]Java adding to ArrayList<String> and then converting to Object[][]

New to Java so here goes: Java 新手,所以这里是:

I am wanting to take my arrayList that I populate by doing this:我想通过这样做来获取我填充的arrayList

ar.add(rs.getString(y)); ar.add(rs.getString(y));

Which produces something like this:产生这样的东西:

value1, value2, value3, ...值1、值2、值3、...

And return it like this:并像这样返回它:

return (Object[][]) ar.toArray();

or

String[][] res = new String[ar.size()][];
return ar.toArray(res);

Though that seems to just put Null, Null, Null all through it.尽管这似乎只是将Null、Null、Null贯穿其中。

When its populating via the ar.add(...) I make sure it does have data ( and it does ).当它通过ar.add(...) 填充时,我确保它确实有数据(它确实有)。

All of this is so I can create a table :所有这些都是为了我可以创建一个

TableModel model = new EditableTableModel(columnTitles, dataEntries);

dataEntries being the Object[][] that it needs. dataEntries是它需要的Object[][]

The original dataEntries code was:原始dataEntries 代码是:

 Object[][] dataEntries = {
     { "value1", "value2", "value3", "value4", "value5", "value6", "value7" },
     { "value1", "value2", "value3", "value4", "value5", "value6", "value7" },
     etc...
 };

Which worked just fine when first testing it.第一次测试时效果很好。 Those values showed up in the jTable as they should.这些值应该出现在jTable中。

This is my full code:这是我的完整代码:

private void initialize() throws Exception {
    [more code here]
    Object[][] dataEntries = reloadData();
    TableModel model = new EditableTableModel(columnTitles, dataEntries);
    table = new JTable(model);
    [more code here]
}

@SuppressWarnings("unchecked")
private Object[][] reloadData() throws Exception {      
    SQLiteJDBCLoader.initialize();
    ArrayList<String> ar = new ArrayList<String>();
    SQLiteDataSource dataSource = new SQLiteDataSource();
    dataSource.setUrl(dbPath);

    try {
        ResultSet rs = dataSource
                      .getConnection()
                      .createStatement()
                      .executeQuery("Select "
                                    + "data_script, "
                                    + "data_status, "
                                    + "data_errors, "
                                    + "data_tester, "
                                    + "data_rundate, "
                                    + "data_tools, "
                                    + "ID "
                                  + "FROM allData");

        int row = 5;
        int col = 6;

        while (rs.next()) {
            for (int x = 1; x < row; x++) {
                for (int y = 1; y < col; y++) {
                    ar.add(rs.getString(y));
                }
            }
        }            
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, ex);
    }

    return  (Object[][]) ar.toArray();
}

What could I possibly be doing wrong?我可能做错了什么?

As good (IMHO) general advice, never use arrays, unless you absolutely have to;作为好的(恕我直言)一般建议,永远不要使用数组,除非你绝对必须; always use Collections.始终使用集合。 Arrays are "high maintenance";数组是“高维护”的; inconvenient and don't play nice with generics.不方便,并且不能很好地使用泛型。

Change your method to return a List of List and your life will be easier.更改您的方法以返回List List ,您的生活会更轻松。 Something like:就像是:

public List<List<String>> getTable() {
    List<List<String>> result = new ArrayList<>();
    for (<every row>) {
        List<String> row = new ArrayList<>();
        // populate row
        result.add(row);
    }
    return result;
}

ArrayList.toArray() returns Object[]. ArrayList.toArray() 返回 Object[]。 While you are expecting 2dimensional result, ArrayList that you have created is 1Dimensional.当您期待二维结果时,您创建的 ArrayList 是一维的。 Try using Datastructure like尝试使用数据结构,如

List<List> or List<Object[]> 

An example should be like:一个例子应该是这样的:

    List<Object[]> arr=new ArrayList<Object[]>();

       while (rs.next()) {
         Object[] temp=new Object[col];
          for (int i = 0; i < col; i++) {

               String columnValue = rs.getString(i);
               temp[i]=columnValue;
           }
       arr.add(temp);

          }    
 return arr.toArray();

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

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