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.

Android

Extra Credits Array Madness

So I set up some code like this, guessing I'm wrong since I get mad compile errors. Is it that I'm providing incorrect parameters for SetText?

public class TextViewArrayMadness extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_text_view_array_madness);

    String[] arrays = {"1","2","3","4","5"};

    TextView window1 = (TextView)findViewById(R.id.textView);
    TextView window2 = (TextView)findViewById(R.id.textView2);



    window1.setText(arrays[1] + arrays[2]+ arrays[3]+ arrays[4]+ arrays[5]);
    window2.setText(arrays[5] + arrays[4]+ arrays[3]+ arrays[2]+ arrays[1]);

}

2 Answers

When you're accessing the values in arrays, remember that the index starts at 0. So arrays[0] would be the string "1", arrays[1] would be "2", and so on. You are trying to access arrays[5] which is not valid. There is no string at index 5 within arrays.

Wow what a facepalm moment, Thanks!