Results 1 to 6 of 6

Thread: Sets?{Resolved}

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Question Sets?{Resolved}

    For some reason when i create a Set multiple ways that contains the elements of a String[] that contains duplicates the Set ends up containing duplicates. This shouldnt be happening. Am i doing somthing wrong?
    Code:
     import java.util.*; 
     
     public class T{ 
       public static void main(String args[]){ 
    
       String[] sarray={"1","4","4","5","4","3","6","4"}; 
     /*
       contains duplicates 
       Collection l = new ArrayList(Arrays.asList(sarray));
       Set s = new TreeSet(l); 
    
       for(Iterator i = s.iterator(); i.hasNext();){ 
          System.out.print(i.next());
       }
    */ 
    
     /*
       contains duplicates 
       Set s = new TreeSet(); 
       for(int i = 0; i < sarray.length; i++){
        s.add(sarray[i]); 
       }
      for(Iterator i = s.iterator(); i.hasNext();){ 
          System.out.print(i.next());
       }
     */
      
      /*
       contains duplicates 
       Set s = new TreeSet(); 
       for(int i = 0; i < sarray.length; i++){
        s.add(sarray[i]); 
       }
      
       Object[] o = s.toArray(); 
       for(int i = 0; i < o.length; i++){
        System.out.print(o[i]);  
       }
     */ 
      }    
     }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    This is very strange.
    public interface Set
    extends Collection

    A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228
    Your code worked for me. But java is ver 1.3.0. I'm sure you're on 1.4.*

  4. #4

  5. #5
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228
    In that case, I won't check it on 1.4.1_01.

    I ran into some interesting results one time myself. It turned out that the solution was to reboot my Windows PC. Funny how that occasionally fixes things.

    So tell me, had you rebooted and then it worked?

  6. #6

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I would like to say that rebooting works all of the time to resolve a problem. Unfortunately it doesn't. At least in this case it did.

Posting Permissions

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



Click Here to Expand Forum to Full Width