• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with sets please  RSS feed

 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make a program that creates 5 sets, where each set has 3 unique numbers in them. The 5 sets are stored in a Treeset.

Problems I am having:
1. The numberSet only outputs one set, because when I clear the number set it erases the set that was put in the numberSet.

I would like to have 5 unique sets (each set has 3 unique number), and be able to store them in another set and be able to access them uniquely. I keep thinking of sets in terms of arrays, so I'm not sure how storing and accessing from a set works. Any help is much appreciated, thanks.


[ July 02, 2006: Message edited by: Zolo Bob ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of clearing the set, you should create a new instance, like
number = new HashSet();

You can then add this set to the numberSet, like this :



[ July 02, 2006: Message edited by: Satou kurinosuke ]
[ July 02, 2006: Message edited by: Satou kurinosuke ]
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help! Just one quick question when I display the output of the numberSet it is all displayed in one long row. Is there a way to iterate through the set and display each of the sets on new lines one at a time?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple loop will do:
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, your a life saver. Could you recommend a good java book on learning set and other data structures? I have the Head First Java which is really good, but it doesn't go into much detail about working with sets and hash stuff.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun's tutorial should be good enough :
http://java.sun.com/docs/books/tutorial/collections/index.html

About sets:
http://java.sun.com/docs/books/tutorial/collections/interfaces/set.html
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!