Results 1 to 4 of 4
- 10-30-2010, 09:20 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
Removing duplicates from double Arrays
Hello,
I'm trying to write a program with a method that takes an array of various double type numbers of any size, then return only the unique numbers (return an array without the duplicates) while maintaining the order.
So input {5, 3, 5, 5, 3, 2, 1, 3, 4} should return {5, 3, 2, 1, 4}.
Here's the code:
But when I run it I get this: :(Java Code:public double[] uniqueNumbers(double[] Numbers) { double[] u = new double[Numbers.length]; double num = Numbers[0]; u[0] = num; int ii = 1; for (int i = 0; i < u.length; i++){ if (Numbers[i] != num){ num = Numbers[i]; u[ii] = num; ii++; } } /*this is the part where I try to make an array u with only the duplicates, then leave the rest with 0s*/ double[] un; int x = 0; for (int z = 0; z < u.length; z++){ if (u[z] == 0){ x = z + 1; } } un = new double[x]; for (int y = 0; y < u.length; y++){ if (u[y] != 0){ [B]un[y] = u[y];[/B] // this is where the error occurs apparently } } return un; /* here I tried to create a new array un that would have the values from u moved over except the 0s */ }
Any idea why?java.lang.ArrayIndexOutOfBoundsException: 0
at xxxxxx.uniqueNumbers(xxxxxx.java:77)
EDIT: Per Fubarable's suggestion, I put in a few println statements here and there, and the 1st part of the code where I create array u, it seems like the input array Numbers just gets copied over to u... :(Last edited by jhong253; 10-30-2010 at 10:05 PM. Reason: Details
-
What line in your code above causes this error to occur? Have you added println statements to see the state of your variables at the time or just before the error occurs?
- 10-30-2010, 10:07 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
Oh sorry about that.
I'm going to edit the original post to reflect on what's where... one sec.
- 10-30-2010, 10:32 PM #4
If I'm not mistaken:
Java Code:double[] un; int x = 0; for (int z = 0; z < u.length; z++){ if (u[z] == 0){ x = z + 1; } } un = new double[x];
What if "u" has 25 values and number [3] and [24] had zeros. "X" would then be equal to... I'm not sure I really understand the use of this statement and then using "X" to declare the size of your next array?Sincerely, Joshua Green
Please REP if I help :)
Similar Threads
-
Java/SQL Removing double data
By Subhero in forum AWT / SwingReplies: 2Last Post: 05-13-2010, 03:44 PM -
Removing Duplicates.
By dashwall in forum New To JavaReplies: 9Last Post: 12-29-2009, 01:03 PM -
removing duplicates from arrays
By bugger in forum New To JavaReplies: 3Last Post: 11-13-2007, 06:11 PM -
Duplicates
By Gambit17 in forum New To JavaReplies: 5Last Post: 11-08-2007, 09:56 AM -
duplicates in iReport
By Heather in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 04:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks