How to declare an array with array size dynamically

You can use the concept of the ArrayList where you don't have to declare the size of an array.

I am attaching the modified  code:

 public class stringDouble
 {
   public double[] stringToDouble(String str)
   {
          String[] ary = str.split(",");
        ArrayList a1=new
ArrayList(Arrays.asList(ary));
        double d=new double[a1.size()];
       for(int i=0;i<ary.length;i++){
System.out.println ("array["+i+"]\tlength("+ary[i].length()+")\t>"+ary[i]+"<");
       }
     for(int j=0;j<ary.length;j++)
     {
         d[j] = Double.parseDouble(ary[j]);
     }
      return d;
   }
  public static void main(String[] argv)
   {
     stringDouble t = new stringDouble();
    double[] d1 = t.stringToDouble("234.3453,345.76,123.345,678.34234");
     for(int j=0;j<d1.length;j++)
    {
       System.out.println("doublearray["+j+"]\t 
> "+d1[j]+" ");
     }
   }
 }

-- Nadeem

Arrays in Java
Java Arrays

Have Java Problem
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.