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 Build a Simple Android App (2014) Coding the Fun Facts Properties of Arrays

We have a facts array as a String, and we have randomNumber as int. Why Android did not show mistake in this case?

I am watching all videos again right now and come to this question. We have a facts array as a String, and we have randomNumber as int. Why Android did not show mistake in this case? Because they are incompatible data types, right? Or I do not know something here?

2 Answers

Hello Adilet Zhusupbekov,

An Array can hold many different types of Objects. For example:

//Array of Integers
int[] intArray = {1, 2, 3, 4};

//Array of Strings
String[] stringArray = {"Hello", "Goodbye", "Good morning"};

Arrays use index's to be able to get the data in a certain location in the array. No matter what the type of array to access the data based on the index we need to pass in a integer.

//Array of Integers
int[] intArray = {1, 2, 3, 4};

//Array of Strings
String[] stringArray = {"Hello", "Goodbye", "Good morning"};

stringArray[0]; // This would give us the String "Hello"
stringArray[1]; // This would give us the String "Goodbye"
string Array[2]; // This would give us the String "Good morning"

Hope this helps!

Thank you, Sam!