Wednesday, March 9, 2011

Dynamic array

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]+" ");
     }
   }
 }

URL:http://www.sap-img.com/java/how-to-declare-an-array-with-array-size-dynamically.htm

No comments:

Post a Comment