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


I need explanation of index.

I think I'm having some trouble understanding what index values are. I would appreciate any help.

2 Answers

Jakob Wozniak
Jakob Wozniak
17,896 Points

Indexes work like this: in a string containing "TeamTreehouse", the characters in the string are indexed. So if you wanted to get the first letter of the string, you would ask it for the character index 0 (this is because indexes start at 0 instead of 1). If you asked for index 3, you would get back "m".

String name = "TeamTreehouse";
System.out.println(name.charAt(0));         /*  This will print "T"   */
System.out.println(name.substring(0,4))  /*  This will print "Team"  */

Thanks all! I think I've got it now.

Ryan Ruscett
Ryan Ruscett
23,309 Points


When I put something into an array, like below. It's not just the strings that it stores. It will store the string and associate it with an index which I can use to get data out and put data into the array. It referes to the location of something inside a data structure. I'll show you.

private String[] words = new String[5];

Now I have an array of strings that takes 5 values. I really have this.

0 []

When I try and store a word into the words array, I must tell java where in the array to store the value. If I want words to be the first value. I would give it an index value of 0, if I want the value to go into the 5th spot I would use an index value of 4. All the index value does is allow me to pick out data inside my data structure and or put data in at a specific spot. I identify that specific spot by use of index values.

words[1] = "Hello"
words[4] = "World"

Now I have this.


What these index values allow me to do. Is retrieve values form my array. If I just said, give me a value array. It would say what value do you want? I can say hey, array, give me the word Hello. Since that defeats the purpose of an array in some ways. If I know the value I want, than I don't need to get it from the array. I can just give it. But if I don't know the value I want, I have to tell the array, give me the "second" value inside the data structure or array. Than it says oh ok, you want the second value. I can do that. In this case you get a null since there is no value.

Does that help make more sense our of it? Let me know if you have additional questions. I can try to explain again.