pass arraylist to Fix

0

I'm trying to pass the values of a arraylist to an array but it tells me that all records are null .

public static void listadedatos() {

for(int i = 0; i < datostabla2.size(); i++) {
    datostabla[i] = (String) datostabla2.get(i);//------------- trato de copiarlo de esta forma y no sirve
    //System.arraycopy(datostabla2, 0, datostabla, 0, datostabla2.size());// trato de copiarloo de esta forma y tampoco sirve 
    //System.out.println("Valor array= " + datostabla2.get(i));
}
for(int i = 0; i < datostabla.length; i++) {
    System.out.println("Valor arreglo= " + datostabla[i]);
}

}
  public static void listadedatos2() {


    try {
        datostabla2.add(result.getObject(i+1));
        for(int i = 0; i < datostabla2.size(); i++) {
            datostabla[i] = (String) datostabla2.get(i);
            //System.out.println("Valor antes= " + datostabla2.get(i));
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
    
asked by Marco Salazar 19.10.2018 в 17:07
source

1 answer

0

To pass a ArrayList<String> to an array ( String[] ) the own class ArrayList provides you with the necessary tools :

String [] arrayDeStrings = miArrayList.toArray(new String[miArrayList.size()]);

By the way, if you use generics (it is recommended), you will not need to cast the elements. In the example that I give you I assume that myArrayList was declared like this:

ArrayList<String> miArrayList = new ArrayList<>();

What would allow you to take out any element like this:

String elem = miArrayList.get(n);
    
answered by 19.10.2018 в 17:42