Results 1 to 6 of 6

Thread: Sets?{Resolved}

Threaded View

  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]);  
       }
     */ 
      }    
     }

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