Results 1 to 8 of 8
  1. #1
    lorreTheGreat is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Decrease array size without losing data

    Hello guys,


    I'm a newbie programmer, and I have been taking a java class for a good couple of months now. I'm using bluej to learn the language.

    My question is simple: do you guys know the right syntax to decrease the size of an array without losing data?

    I have created an array, and mutated it slightly, now I want to get rid of the last value in the Array, but have no idea how to do it.
    I figured it would involve an "in-between" array, to stock all my values, while I remake my array (which I have put in a field), but I can't get it working.

    Many thanks in advance,


    Lorre The Great

    PS should you require more info, I'll gladly supply you with it

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Decrease array size without losing data

    You normally don't use arrays for such a requirement. Have a look at the List implementations.
    If you still want to use arrays, post your code with [code] [/code] tags.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    lorreTheGreat is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Decrease array size without losing data

    Java Code:
    public class Fibo
    {
    
        private int fibon[];
        
        private int numberOfElements;
        
     
      
        public Fibo()
        {
            numberOfElements= 10;
            fibon= new int[numberOfElements];
            fibon[0]=1;
            fibon[1]=1;
            
            
            for(int i=2;i< numberOfElements; i++)
            {
                         
                fibon[i]= fibon[i-1]+ fibon[i-2];
    
            }
        }
        public Fibo(int numberOfElements)
        {
            this.numberOfElements= numberOfElements;
            fibon= new int[numberOfElements];
            fibon[0]=1;
            fibon[1]=1;
            
            
            for(int i=2;i< fibon.length; i++)
            {
                         
                fibon[i]= fibon[i-1]+ fibon[i-2];
    
            }
        }
        
        public void verwijderTerm(int Term){
            int begin;
            int middle;
            int end;
            int position;
            int i;
      
            
            end= fibon.length-1;
            begin=0; 
            
            while(end > begin){
                middle= (begin + end) /2;
                if(Term > fibon[middle]){
                    begin= middle + 1;}
                    else{
                        end= middle;
                    }          
            }
            if (Term== fibon[begin]){
                        position= begin;                
                    }
                    else
                    {
                        position= -1;
                    }
            
                    if(position > 0){
                         for(i=position; i < numberOfElements-1; i++){
                        
                            fibon[i]= fibon[i+1];  
                            
                            
                                                           
                       
                       
                        }
                        
                        
                        
                        numberOfElements--;
                        
                        
                        
                        
                        
                        
                       
                        
                        
                        
                       
                                       
                        
                        
                    }
                    
                    
                    
            
            
            
            
            
        }
     
    }

    Here's the code: it's about making a fibonacci sequence, then adding a method to remove one of the numbers, and slide the other numbers back.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Decrease array size without losing data

    I have no idea what exactly you're trying in that verwijderTerm method. Can you explain what you intend to do? Getting rid of just the last value in an array is just a few lines of code.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    lorreTheGreat is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Decrease array size without losing data

    First, binary search for the number I want to delete. Then I use a for loop to "slide" back the other numbers, into the place of the one I deleted (it's an assignment so we had to do it). So the number on place six is placed in the place of the number on position five, and so on.

    The if's there to know whether or not the term we are looking for is in the fibonacci sequence.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Decrease array size without losing data

    Does that part work correctly? If so you just have to create a new array with newLength= fibon.length-1 and copy over all values up to newLength. After that you assign the reference of the new array to fibon and you're done.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    lorreTheGreat is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Decrease array size without losing data

    Yep, it does. I'll try that.

    Thanks for your time :)

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,912
    Rep Power
    5

    Default Re: Decrease array size without losing data

    Also, something that might make your development easier and more clear is to write separate methods for finding the value and removing it from the array.

    Once you've written the code (method) to find the term and return it you could do something like:

    Java Code:
    public int[] deleteElement(int [] array, int indexOfTerm) {
         int[] newArray = new int[array.length-1];
         ...
    // rest of logic to populate array
         ...
         return newArray;
    }
    Of course it can be named anything. Just make certain the index is within the array bounds and catch and handle any errors.

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Losing data from array - not sure why
    By neveser in forum New To Java
    Replies: 8
    Last Post: 12-02-2012, 08:19 AM
  2. Replies: 1
    Last Post: 08-16-2012, 04:51 PM
  3. Calculate size of data structure
    By GabiCRC in forum Advanced Java
    Replies: 0
    Last Post: 01-13-2012, 01:18 PM
  4. Swing Applet losing dirty data
    By ptreves in forum Java Applets
    Replies: 1
    Last Post: 09-25-2011, 04:30 PM
  5. gridbaglayout: increase/decrease size of components.
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •