Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Sets
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
kevin Noronha




PostPosted: Tue Mar 11, 2008 2:33 pm   Post subject: Sets

what code would you use to print out contents from a set
Example:
HashSet set=new HashSet();
set.add(5);


How can you print out the value 5??
Sponsor
Sponsor
Sponsor
sponsor
HeavenAgain




PostPosted: Tue Mar 11, 2008 3:59 pm   Post subject: RE:Sets

depends on how you want to work this, there is a contains method, which you can check if 5 is in there, so we do
code:
if (set.contains(5))
    System.out.println(5);
//or use the toString method
System.out.println(set);
//or converting it to array, and treat it like an array
set.toArray();
any more? I'm not sure... if there is a better answer, let me know Smile
McKenzie




PostPosted: Tue Mar 11, 2008 4:39 pm   Post subject: Re: Sets

With a HashSet you can can test if 5 is in your set e.g.
set.contains(5)

You can also print everything like:
for(Integer i : set)
System.out.println(i);
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: