Results 1 to 3 of 3
  1. #1
    xtrmi is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default Having trouble removing from TreeSet

    Take a look. The remove operation never returns TRUE. What am I doing wrong?

    Java Code:
    public static double closestPoints (Point [] xQueue) {
                
                Point closestLeft, closestRight, current, check,tmp;
                int c,n,t;
                double delta, newDelta;
                yComparator yC = new yComparator();
            // initX **************************
                Arrays.sort(xQueue); // sort xQueue by X-coordinate
                
           // initY  **************************
                TreeSet<Point> yTree = new TreeSet<Point>(yC); // Creates empty tree set
                
                closestLeft = xQueue[0];
                closestRight = xQueue[1];
                delta = distance(closestLeft, closestRight);
                yTree.add(xQueue[0]);
                yTree.add(xQueue[1]);
                c = 2;
                t = 0;
                n = xQueue.length;
                
                while ( c < n ) {
                    
                    current = xQueue[c];
                    while ( t < c && (double)(current.x - xQueue[t].x) >= delta) {
                        if ( yTree.remove(xQueue[t]))
                        System.out.println("REMOVED ----> " + xQueue[t].x + ":" + xQueue[t].y);
                        t = t + 1;
                    }

  2. #2
    xtrmi is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default

    Disregard this I messed with the comparator in a weird way and it affected the remove method. Curious why if anybody knows.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I would expect yTree.remove() to return false becuase, as the comment in your code notes, you create an empty tree. You add the end points from the array but no other points.

    Even if you "add" the other points whether they are actually added (and therefore whether remove() will return true) is going to depend on how you implement the comparator. See my reply in your other thread.

Similar Threads

  1. trouble in removing a value
    By jacline in forum New To Java
    Replies: 5
    Last Post: 03-20-2009, 05:56 PM
  2. TreeSet Demonstration
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:34 PM
  3. Working with HashSet and TreeSet
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:34 PM
  4. ClassCastException in TreeSet
    By pHew in forum New To Java
    Replies: 2
    Last Post: 01-16-2008, 12:20 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
  •