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 Build an Interactive Story App (Retired) Finishing the User Interface Loading the First Page

Saurabh B
PLUS
Saurabh B
Courses Plus Student 2,880 Points

Use of []

What is the use of [] in the code: return mPages[pageNumber]

[] is only used for arrays, isn't it?

1 Answer

Harry James
Harry James
14,780 Points

Hey Saurabh!

Yes, the [] is used for arrays. Inside of the [], we pass in the integer of the index in the array we want to access.

So, what we have here is we have our mPages array - this stores all of the different pages we have. We then have pageNumber - the integer that says what page we want.

So, when the code runs, it will actually look something like this (Though we never see this):

mPages[0]
// Get page at index 0.
mPages[1]
// Get page at index 1.
mPages[2]
// Get page at index 2.
// etc...

So, wrapped up now, it says "In mPages, get the object at the index we pass in and give it to us".


Hopefully this should clarify this for you but, if there's still something you don't quite understand, let me know and I'll try my best to explain it for you :)