Posted: 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
HeavenAgain
Posted: 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
McKenzie
Posted: 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);