0

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
Makoto
  • 104,088
  • 27
  • 192
  • 230
Joseph
  • 11
  • 2

1 Answers1

1

If you don't know the number of elements, you should be using a List. I'd go with an ArrayList or an ArrayDeque:

List<Object[]> results = new ArrayList<>();
Object[] set = new Object[6];

Once you've finished creating your Object[] with the 6 results, call results.add(set);

Jacob
  • 91
  • 1
  • 9
  • what is better choice using List Or LinkedList ..Jacob – Joseph Aug 27 '16 at 08:43
  • Your default go-to list should be ArrayList. LinkedList is useful in very special circumstances. Both of these implement the List interface. – David Soroko Aug 28 '16 at 20:15
  • ArrayLists are usually my preferred List type. I looked into it more and it appears LinkedLists usually have very limited uses and ArrayLists have generally better performance [link](http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist). This is my recommendation. – Jacob Aug 30 '16 at 15:20