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 trialAlexander Moise
1,410 PointsArrays and declaring variables at the same time
Hello everyone and Ben!!! it's Alex and i believe I missed on how to declare a string variable and initialize it to the first element of an array can anyone help please? Thankyou!!!
3 Answers
Andrew Nash
344 PointsIf you have an array of String objects like this: String[] stringArray = new String[] {"one", "two", "three"};
You can initialize a String variable to the first element like this: String firstString = stringArray[0];
The value of firstString should be set to "one". Hope that helps!
Hiroki Hokari
5,308 PointsInitializing Array,,, doesn't ring a bell. I think the way to put something in array is to "add" it after creating the skelton or structure of it.
For example, (some kind of)Array array = new Array(); or String[] string = new String[(length)]; If this is called initializing, this is pretty much it. And then array.add(something); or string[2(which means the third element)] = "Something";
Alexander Moise
1,410 PointsAndrew Nash your awesome you dont know how long it took me to get through that single objective. Thankyou your the man lol!!! Hiroki thanks for putting out the time to try to answer my question but for future reference if you ever need any help on this matter just look to Andrew Nash's response Thanks my "friend"
Andrew Nash
344 PointsNo problem! I've been working with Java for a while. If you have any other Java related questions as you go I'd be happy to try and help.