Results 1 to 8 of 8
- 02-27-2013, 09:59 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 4
- Rep Power
- 0
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
- 02-27-2013, 10:07 AM #2
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
- 02-27-2013, 10:11 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 4
- Rep Power
- 0
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.
- 02-27-2013, 10:26 AM #4
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
- 02-27-2013, 10:31 AM #5
Member
- Join Date
- Feb 2013
- Posts
- 4
- Rep Power
- 0
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.
- 02-27-2013, 10:52 AM #6
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
- 02-27-2013, 12:10 PM #7
Member
- Join Date
- Feb 2013
- Posts
- 4
- Rep Power
- 0
Re: Decrease array size without losing data
Yep, it does. I'll try that.
Thanks for your time :)
- 02-27-2013, 03:24 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 901
- Rep Power
- 1
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:
Of course it can be named anything. Just make certain the index is within the array bounds and catch and handle any errors.Java Code:public int[] deleteElement(int [] array, int indexOfTerm) { int[] newArray = new int[array.length-1]; ... // rest of logic to populate array ... return newArray; }
JimThe Java™ Tutorial
YAT -- Yet Another Typo
Similar Threads
-
Losing data from array - not sure why
By neveser in forum New To JavaReplies: 8Last Post: 12-02-2012, 07:19 AM -
How to decrease the HttpClient url connectivity time to a server ?
By aghilkrishna in forum NetworkingReplies: 1Last Post: 08-16-2012, 03:51 PM -
Calculate size of data structure
By GabiCRC in forum Advanced JavaReplies: 0Last Post: 01-13-2012, 12:18 PM -
Swing Applet losing dirty data
By ptreves in forum Java AppletsReplies: 1Last Post: 09-25-2011, 03:30 PM -
gridbaglayout: increase/decrease size of components.
By newtojava7 in forum New To JavaReplies: 2Last Post: 01-28-2008, 07:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks