How would I append an Object[][]
to another Object[][]
?
I have created Object dhadj[][] = new Object[0][6]
, which I've initialized to 0 because I don't know the number of rows, but I do know the number of columns.
In a loop I create another Object[][]
called dha
, and search for some data in the database. My intention is, at the end of every loop I want to add dha
to dhadj
using an append function. However, I get an error; there is a problem with converting array to object return (Object[][]) with temp.toArray()
.
What can I do to fix this? Is there another approach?
Here is my code:
Object dhadj[][] = new Object[0][6];
v = vector;
ro = size;
for (int i = 0; i < ro; i++) {
int d;
d = Integer.valueOf(v[i]);
query = "SELECT * FROM hadj WHERE id=?";
try (PreparedStatement preparedStatement = c.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY)) {
preparedStatement.setInt(1, d);
ResultSet rs = preparedStatement.executeQuery();
if (rs != null) {
rs.last();
int siz = rs.getRow();
rs.beforeFirst();
ResultSetMetaData rsMeta = rs.getMetaData();
int a = 0;
Object dha[][] = new String[siz][6]; //the neew object dha
while (rs.next()) {
dha[a][0] = rs.getString("id");
dha[a][1] = rs.getString("idh");
dha[a][2] = rs.getString("ticket");
dha[a][3] = rs.getString("montant");
dha[a][4] = rs.getString("datei");
dha[a][5] = rs.getString("nombrep");
a = a + 1;
}//end while
dhadj = appendValue(dhadj, dha); //here I add it using fuction
} else {
dhadj[0][0] = Integer.toString(d);
for (int ii = 1; ii < 6; ii++) {
dhadj[0][ii] = "";
}
}
}//end try
}//end for v
This is the function I'm using to append the arrays together:
private Object[][] appendValue(Object[][] obj, Object[][] newObj) {
ArrayList<Object> temp = new ArrayList<Object>(Arrays.asList(obj));
temp.add(newObj);
return (Object[][]) temp.toArray();
} //end appendvalue