• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with sets please

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple loop will do:
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic