0

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.

Micho
  • 3,929
  • 13
  • 37
  • 40
  • 2
    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. – Ousmane D. Apr 25 '17 at 03:32
  • You could make a `List>` instead since you know how to access a list. It's not clear why you need an array – OneCricketeer Apr 25 '17 at 03:35
  • 1
    Use `ArrayList` everywhere you have the plain `ArrayList`. A plain `ArrayList` is called a raw type and it's the same as not using generics. – Radiodef Apr 25 '17 at 03:36
  • What is the purpose of `reg`? You can skip that arraylist and use `new String[nColumnas];`... – OneCricketeer Apr 25 '17 at 03:38
  • Thanks for you answers, as for not using reg you're right, i could just use new String, thanks. As for using a List I 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

0 Answers0