I have this piece of code:
ArrayList <String[]> seleccionar(String query){
ArrayList lista = new ArrayList <>();
try{
//Obtener datos de la base
Statement cmd = cnx.createStatement();
ResultSet res = cmd.executeQuery(query);
ResultSetMetaData resMetaData = res.getMetaData();
int nColumnas = resMetaData.getColumnCount();
while(res.next()) {
ArrayList reg = new ArrayList <>();
for (int i = 1; i <= nColumnas; i++) {
reg.add(res.getString(i));
}
String[] registro = new String[reg.size()];
for (int i = 0; i < reg.size(); i++) {
registro[i] = reg.get(i).toString();
//System.out.print(registro [i] + " ");
}
lista.add(registro);
//System.out.println("Añadido a lista.");
}
res.close();
cmd.close();
} catch (SQLException ex) {
System.err.println("Error en el comando a BD");
}
return lista;
}
But ArrayList
makes every String[]
added to lista
into an Object
, and I can't use it as a String[]
anymore. I need to be able to access every String
inside the String[]
inside the Arraylist
.
What should I do? Is using ArrayList
the wrong choice? Or am I missing some functionality on ArrayList
?
All my knowledge is from experimentation and concepts read from here and there, so I have quite a few holes in it.
>` instead since you know how to access a list. It's not clear why you need an array
– OneCricketeer Apr 25 '17 at 03:35I dont know if its valid because in this particular exercise i'm given the classes with the methods and method types, and i'm not supposed to change it from an ArrayList.
– Jafet Cardenas Apr 25 '17 at 05:32