0

I'm searching for this problem since days and I can't seem to find a proper answer. So I have an ArrayList of Arrays in form of Objects (ArrayList) filled with the result set of a SQL string. My goal is to convert the result of only one row into a one-dimensional Array of Objects.

I have tried several times my research results from this website, but the only knowledge I've made was how to convert a one-dimensional ArrayList to a simple array. I hope you guys can help me in some ways. Thanks!

My table is looking like this:

sizeid | diameter
   1   |    32
   2   |    40

With the SQL-command "SELECT diameter FROM size" I'll get those two numbers 32 and 40, stored in a simple 2x1 table.

|  32  |
|  40  |

Now I want to have them in a one-dimensional Array.

the method to convert:

private Object[] getSizes() {
   query = new SQLquery();
   query.setSQLstring("SELECT diameter FROM size");
   query.runQuery();

   List<Object> sizeList = query.getRowData();
   Object[] sizes = new Object[sizeList.size()];

   // here I want to convert the List to an array..

   return sizes;
}

the method to generate the ArrayList of the class SQLquery:

public void outputResult() {
    try {
       if (result != null) {
          ResultSetMetaData meta = result.getMetaData();
          columns = meta.getColumnCount();
          columnNames = new Object[columns];
          for (int i=1; i<=columnNames.length; i++) {
             columnNames[i-1] = meta.getColumnName(i);
          }
          List<Object> rowData = new ArrayList<Object>();
          while (result.next()) {
            Object[] row = new Object[columns];
            for (int j=0; j<= columns; j++) {
               row[j-1] = result.getString(j);
            }
            rowData.add(row);
         }
      }
   } catch (SQLexception e) {
      System.out.println(e);
   }
}

and the simple get-method of the class SQLquery:

public List<Object> getRowData() {
   return rowData;
}
4
  • Object[] obj = myArrayList.get(0) Commented Apr 18, 2014 at 10:29
  • No, I think he wants an array with all the single-valued results from the query. Commented Apr 18, 2014 at 10:29
  • eclipse responses me this error message: "type mismatch: cannot convert from Object to Object[]" Commented Apr 18, 2014 at 10:33
  • Something's wrong here: for (int j=0; j<= columns; j++) row[j-1] = result.getString(j); I believe j should start from 1. Commented Apr 18, 2014 at 10:38

2 Answers 2

1

Apparently, your getRowData() method returns a List<Object> whereas it actually contains arrays. Although this looks strange since you say it always returns a list of arrays, I'll assume that's what you have.

You need a loop which goes over these "objects", downcasting them to Object[], and taking the first member.

Object[] sizes = new Object[sizeList.size()];
int i = 0;
for (Object o : sizeList) sizes[i++] = ((Object[]) o)[0];
return sizes;
Sign up to request clarification or add additional context in comments.

2 Comments

Why is it strange that the items of the list of objects are arrays? Actually every item of the list of object is representing a resulting row of the SQL query: a row can contain multiple values (the column): this is only a special case where the query selects only one columns, and therefore we have arrays with only one item.
@Nicola It is not strange that the items in the list are arrays. What is strange is that the type of the said list is List<Object>.
0

You could use the approach what Marko Topolnik suggested.

However I would suggest to write your own "Table" class, which parses the result set and puts the values into an N*M dimensional array (or List) depending on your query. This way you can store meta information like the size of N or M dimension, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.