Array must has length but i want without it

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Formula
    New Member
    • Aug 2008
    • 11

    Array must has length but i want without it

    Hi all,i have Big problem mmm that the array must has the length but my program cann't be. Becase the array depend on the loop so i don't want to give a number like 20 or 100 .

    I searched through my book and didn't found anything.

    So in java i have to add number to my array,it work greet but i donn't want to do it.
    Please Help.
    Code:
    import javax.swing.JOptionPane;
    
    public class  ConvertDtoB{
    	
    	
      public static void main(String arg[]){
      	
       int ircounter=-1;
       int[] iarray=new int[20];
       int[] rarray=new int[20];
       
       float number;
       int asas,f1,r2;
       number=(int)Float.parseFloat(JOptionPane.showInputDialog("Enter number"));
       System.out.println("The binary of the following decimal number "+number+" is:");
       asas=(int)number;	
       
       int d2=2;
    	while(d2==2)
    	{
    		f1=asas/2;
    		r2=(int)asas-(f1*2);
    		ircounter++;
    		iarray[ircounter]=f1;
    		rarray[ircounter]=r2;
    		System.out.println(rarray[ircounter]);
    		
    		if(f1!=0){asas=f1;}else{break;}
    	}
      }
    
    }

    I do it in php like this.
    Code:
    <?php
    class d2b{
    var $iarray=array();
    var $rarray=array();
    
    	function d2b($asas){
    	$f1=$asas/2;
    	$f1=intval($f1);
    	$r2=($asas)-($f1*2);
    
    	array_push($this->iarray,$f1);
    
    //add value to the binary array
    	array_push($this->rarray,$r2);
    //if the reminder != 0 loop it
    	 if($f1!=0){d2b::d2b($f1);}
    	}
    
    }
    $D2b=new d2b(125);
    print_r($D2b);
    ?>
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    As you have found out already: arrays are kind of stupid: they have a fixed length and when you don't know that length in advance you're in trouble. Better use Lists or ArrayLists in particular; they give you all the 'aray' functionality and their length is adjusted on the fly; read their API documentation.

    kind regards,

    Jos

    Comment

    • Formula
      New Member
      • Aug 2008
      • 11

      #3
      Thanks man .Even though I alredy use it in another language(i won't say which) ,but it didn't come to mined.
      So I gave you this appliction for convert from decimal to binary it really usefull .Thanks again.
      Code:
      import java.util.*;
      import javax.swing.JOptionPane;
      
      
      public class  DecimalToBinary{
      	
        public static void main(String arg[]){
        	
        	List iarray = new LinkedList();    
          iarray = new ArrayList();         
        	List rarray = new LinkedList();    
          rarray = new ArrayList();     
          int ircounter=-1;
          float number;
          int asas,f1,r2;
          number=(int)Float.parseFloat(JOptionPane.showInputDialog("Enter number"));
          System.out.println("The binary of the following decimal number "+number+" is:");
          asas=(int)number;	
         
         int d2=2;
      	while(d2==2)
      	{
      		f1=asas/2;
      		r2=(int)asas-(f1*2);
      		
      		ircounter++;
      		
      		iarray.add(ircounter,f1 );
      		rarray.add(ircounter,r2 );
      		
      		System.out.println(rarray.get(ircounter)+":"+iarray.get(ircounter));
      		
      		if(f1!=0){asas=f1;}else{break;}
      	}
      	
        }
      
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Also have a look at the API of the several Integer.toStrin g(...) methods.

        kind regards,

        Jos

        Comment

        Working...