Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

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,307 Points

Hey,

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 []
1[]
2[]
3[]
4[]

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.

0[Hello]
1[]
2[]
3[]
4[World]

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.