14 Replies Latest reply: Nov 3, 2006 3:43 PM by JosAH RSS

    Convert ArrayList to array

    807607
      Hi,

      What would be the best solution for converting an ArrayList to and array?

      Currently, I use (the ArrayList contains Strings)

      ArrayList l = new ArrayList();
      l.add(...);
      ...
      String [] v = new String [l.size()];
      v = (String []) l.toArray(v);

      But I think this could be better or shortes. Specially, following did not work:
      String [] v = (String []) l.toArray(); // ClassCastException
      Where would the toArray() (no param) be useful?

      Best regards,
      Daniel