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

Android Build a Simple Android App (2014) Improving Our Code Adding More Colors

Noman Rafiq
Noman Rafiq
708 Points

What if i have more than 10 Facts.

If i have a wide list of Facts, lets say more than 1000.. Would i have to put all those facts in FactBook like other 10 Facts? Everytime I want to add new facts, i will have to update Factbook? would there be any affect on Speed of app if i add more data in Factbook?

4 Answers

Christopher Augg
Christopher Augg
21,223 Points

Noman,

Those are very interesting and thoughtful questions. Lets dive in!

1) Would i have to put all those facts in FactBook like other 10 Facts?

A) No, even if you leave the project in the state it is in as a simple application with a String array of facts in the FactBook class, you will pretty much have to update that array with the 1000 facts. However, this could be done in many ways. Especially considering that it is public and violating encapsulation. Therefore, you could add facts within the class as it is currently done, add facts in the class using a parser to parse a text file, or even create a new array in the OnCreate method of the FunFactActivity class like so:

      String[] moreFacts = new String[12]; //or 1000

                for(int i = 0; i < mFactBook.mFacts.length; i++) {  //copy the other facts into new array
                    moreFacts[i] = mFactBook.mFacts[i];
                }

                mFactBook.mFacts = moreFacts; //references the new array back into the FactBook class

                //add new facts
                mFactBook.mFacts[10] = "Android Rocks!";
                mFactBook.mFacts[11] = "You Rock!";

This is not a good way as you might have realized. The project here is for learning and not on the scale of a commercially viable product. Later in the course, we will learn about persistent data etc. Databases are a great way for doing this for instance. Also, I recommend taking the java course unless you already have a solid understanding of Java and Object Oriented Programming.

2) would there be any affect on Speed of app if i add more data in FactBook? A) Welcome to time complexity. Computer science terminology that is above and beyond the scope of this assignment. The basic answer is no. Why? Because the random method inside the FactBook class generates a number and that number is used to access the array in constant time. When I say I want the 50th item in an array for instance, then I just access it at the intended index of the array. Even when we loop over 1000 items in an array one after the other, we are going through them in linear time. That is also a good place to be with time complexity. It is when we get to quadratic or exponential time complexity that we want to think about our implementation. Of course, we also need to think about space complexity sometimes as well. Do we really want a String array to hold 1 billion facts? 1 Trillion? There are some good youtube videos on the subject and even MIT opencoursweare. However, I would concentrate on the basics of being able to design and develop using code skills before moving toward those concepts.

I hope this helps.

Regards,

Chris

Akash malhotra
Akash malhotra
3,025 Points

The more facts you put, obviously the more memory it will take. If you want to store a lot of items, you could save the data in a SQL database. More here: http://developer.android.com/training/basics/data-storage/databases.html

Noman Rafiq
Noman Rafiq
708 Points

Thank you Akash, i am at a beginner level yet but thats what i was trying to ask if i am attach a backend database. Thanks again. will learn more and use this method.

regards

Noman Rafiq
Noman Rafiq
708 Points

Thank you so much Chris. Honestly saying I really tried to get Java course here, but i couldn't get with it because The java course here on TeamTreehouse uses a different method, maybe I am unable to explain or understand but i found it different. To compile manually after every line, some different Commands like "printf" and not "System.out". etc

but i am trying to find material online and trying my best to Not give up. :)

Christopher Augg
Christopher Augg
21,223 Points

Np Noman. Have you tried Udemy? There is a great free java course there:

https://www.udemy.com/java-tutorial/#/

Also, the practice it website at my University is very helpful and free:

http://practiceit.cs.washington.edu

Just add Treehouse as your school if you are not attending any school when you sign up. It follows both the second and third addition of the following book:

http://www.amazon.com/Building-Java-Programs-Basics-Approach/dp/0136091814/ref=sr_1_1?ie=UTF8&qid=1438127235&sr=8-1&keywords=Building+Java+Programs

I hope those help.

Regards,

Chris

Noman Rafiq
Noman Rafiq
708 Points

Wow... Thank you so much.. I am going to sign up for Udemy for Java and Practiceit. I have Head First Java Book but I think i will have room for many more. :p

thanks again for your kind help.