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

Android Handling Clicks

Kareem Jeiroudi
Kareem Jeiroudi
14,984 Points

DISCUSSION: Why don't you use `imageIndex = (imageIndex+1) % imageResIds.length`?

Instead of using

imageIndex+=1;
if (imageIndex == imageResIds.length) {
imageIndex = 0;
}

On could type:

imageIndex = (imageIndex+1) % imageResIds.length;

Saves time and ensures the index will return to zero whenever it reaches the maximum length/size. I find it easier too, whenever I need to make changes to whether the increment must occur before or after updating the status of an object. It's definitely not a big difference, I just learned this in one of my classes, where we worked with RingBuffers.

1 Answer

How about this subtle difference imageIndex = (++imageIndex) % imageResIds.length;