Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

How do i compare the last item of a HashSet with something else?

basically I need my animation to stop as soon as the last ball in my HashSet goes past the Xposition, which is 550.

so far this is what I have but it is wrong

if(ball[ballSet.size()].getXPosition() >= 550) { finished = true;

2 Answers

It looks like you're confusing array, Set, and List. The first problem is you're using an array lookup on an object that isn't an array.

Second, the concept of "last item" isn't compatible with the concept of Set. Think of a Set more like a bag or sack of unique items. An item can be in the bag or not but it doesn't matter how the items get jumbled together. The different implementations like HashSet and TreeSet are just different ways to fit the items into the bag.

It looks like you should be using an array (the number of balls you need is fixed throughout the program), a List (the number of balls you need changes dynamically), or reconsider what "last ball" means in your program.

Thank you very much Seth Kroger your explanation helped a lot.