简体   繁体   English

转换ArrayList <String[]> 转换为String [] []或Object [] []

[英]Converting ArrayList<String[]> to String[][] or Object[][]

I wanted to dynamically generate an array of fixed sized String[] (String[][] object) , filled w. 我想动态生成一个固定大小的String [](String [] []对象)的数组,该数组用 w填充。 Strings of rows values from db to create JTable. 从db创建JTable的行值的字符串。

To do that, I used ArrayList of String[], dynamically filled it up. 为此,我使用了String []的ArrayList,将其动态填充。 And then convert it to Array using list.toArray(). 然后使用list.toArray()将其转换为Array。 But .toArray() only convert the list into single dimension Array either Object[] or T[]. 但是.toArray()仅将列表转换为Object []或T []的一维Array。

I need String[][] / Object[][] to use the JTable constructor. 我需要String [] [] / Object [] []使用JTable构造函数。

The code 编码

Object[][] dlist = (Object[][]) al.toArray();

generates: java.lang.ClassCastException: [Ljava.lang.Object; 生成:java.lang.ClassCastException:[Ljava.lang.Object; cannot be cast to [[Ljava.lang.Object; 无法转换为[[Ljava.lang.Object;

ers = pdao.getEmployeeResultSet(prs.getInt("PROJ_ID"));
ArrayList<String[]> alist = new ArrayList<String[]>();
while (ers.next()){
    String eid = ers.getString("EMP_ID");
    String ename = ers.getString("EMP_NAME");
    String gend = ers.getString("GENDER");
    String bd = ers.getString("BIRTHDATE");
    String addr = ers.getString("ADDRESS");
    String city = ers.getString("City");

    String[] str = {eid, ename, gend, bd, addr, city};
    alist.add(str);
}
Object[][] dlist = (Object[][]) al.toArray();
String[] cnames = {"EMP_ID","EMP_NAME","GENDER","BIRTHDATE","Address","City"};

jtable = new JTable(dlist, cnames);

I used the tuturial on : http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SimpleTableDemoProject/src/components/SimpleTableDemo.java to create JTable. 我在以下网址使用了教程: http ://docs.oracle.com/javase/tutorial/displayCode.html?code= http ://docs.oracle.com/javase/tutorial/uiswing/examples/components/SimpleTableDemoProject/src/ components / SimpleTableDemo.java创建JTable。

Simply use 只需使用

String[][] dlist = alist.toArray(new String[][]{});

The array you pass to the method can alternatively be used as the actual array that will be returned if you know the size (which you do with alist.size() ), but it's really useful for the type of array that you want. 如果您知道大小(使用alist.size() ),则可以将传递给该方法的数组用作将返回的实际数组,但这对于所需的数组类型确实有用

You can actually confirm this with 您实际上可以通过以下方式确认

String[][] holder = new String[alist.size()][];
String[][] returned = alist.toArray(holder);
System.out.println(holder == returned);

will print 将打印

true

Now, obviously, since arrays are covariant, you can also do 现在,显然,由于数组是协变的,所以您也可以

Object[][] dlist = al.toArray(holder);

I would just create the double array myself, if you really wanted to: 如果您确实想执行以下操作,我将自己创建一个双数组:

public static String[][] getDArray(final ArrayList<String[]> aList) {
    final int size = aList.size();
    final String[][] ans = new String[size][];

    for(int i = 0; i < size; ++i)
        ans[i] = aList.get(i);

    return ans;
}

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

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