Results 1 to 4 of 4
  1. #1
    jhong253 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default 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:
    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 */
    
      }
    But when I run it I get this: :(

    java.lang.ArrayIndexOutOfBoundsException: 0
    at xxxxxx.uniqueNumbers(xxxxxx.java:77)
    Any idea why?


    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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  3. #3
    jhong253 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    Oh sorry about that.

    I'm going to edit the original post to reflect on what's where... one sec.

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    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

  1. Java/SQL Removing double data
    By Subhero in forum AWT / Swing
    Replies: 2
    Last Post: 05-13-2010, 03:44 PM
  2. Removing Duplicates.
    By dashwall in forum New To Java
    Replies: 9
    Last Post: 12-29-2009, 01:03 PM
  3. removing duplicates from arrays
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 06:11 PM
  4. Duplicates
    By Gambit17 in forum New To Java
    Replies: 5
    Last Post: 11-08-2007, 09:56 AM
  5. duplicates in iReport
    By Heather in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 04:42 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
  •