1

I wanted to dynamically generate an array of fixed sized String[] (String[][] object), filled w. Strings of rows values from db to create JTable.

To do that, I used ArrayList of String[], dynamically filled it up. And then convert it to Array using list.toArray(). But .toArray() only convert the list into single dimension Array either Object[] or T[].

I need String[][] / Object[][] to use the JTable constructor.

The code

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

generates: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[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.

Sotirios Delimanolis
  • 252,278
  • 54
  • 635
  • 683

2 Answers2

1

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.

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);
Sotirios Delimanolis
  • 252,278
  • 54
  • 635
  • 683
-2

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;
}
Jared
  • 946
  • 5
  • 8
  • This method is not typesafe. `String[][] s = getDArray(list);` will result in a ClassCastException. – McDowell Apr 04 '14 at 07:47
  • Making the method generic appears to be more-or-less not be possible in Java: http://stackoverflow.com/questions/18110993/creating-generic-two-dimensional-array-using-class-object . Which is unfortunate, but a pretty simple fix for the above code (just don't make it generic). – Jared Apr 04 '14 at 16:42
  • And I really don't understand why this would not be possible. Every object in Java is a pointer (the exact same size pointer for a particular JVM). So creating a generic array of pointers should not be difficult. I understand if this were C and they were structs--you wouldn't know how big to make each slot for the array, but that's not how Java implements an array. Think about declaring a `new String[10]`--a `String` is a variable sized data structure, so it cannot possibly create room for 10 arbitrary `String` objects--instead it just makes 10 slots for `String` pointers. – Jared Apr 04 '14 at 16:52