12 Replies Latest reply: Jun 21, 2007 4:02 AM by masijade RSS

    int Array's and variable length

    807605
      for Strings i simply do:
      String datanames[] = {};
      ArrayList aList = new ArrayList(Arrays.asList(datanames));
      aList.add("test");
      aList.add("crap");
      datanames = (String[]) aList.toArray(new String[aList.size()]);
      However the same can't be done with int's because an ArrayList cannot store int's and as a result i need a reference back to the original int array, much like the string example above.


      public class testing {
      
           /**
            * @param args
            */
           public static void main(String[] args) {
                // TODO Auto-generated method stub
                int datavalues[] = {};
                System.out.println(datavalues.length);
                datavalues[datavalues.length] = 1;
           
                
           }
      Obviously, with the above i get index out of bounds simply because datavalues hasn't been initialized.

      Quite simply put: i need a way of appending an int array with defining the length.

      Any ideas?

      Message was edited by:
      spear_arrow