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 (retired 2014) Getting Started with Android Introduction to Methods and Classes

Pizza order example

In the section of getting started with android -> introduction to methods and classes there is an example about pizza's and toppings in which we get a toppings string that represents the toppings on the current pizza. Wouldn't it make more sense if we'd use an array in this situation ?

2 Answers

Dervis OGUNC
PLUS
Dervis OGUNC
Courses Plus Student 2,916 Points

I think it really depends on the application. If order application has checkboxes it makes more sense to use array. But if there is a textbox that you can type your toppings and split by comma, using string will be ok. In addition, If I am not wrong, string data type requires less memory space than array does. So you can carry toppings data as string toppings = "olive, onion, corn";

Firstly the example is used to demonstrate the use of methods (section of code that is called upon by the instantiated object of the class). So in that case it makes prefect sense.

Now to answer your question.

Consider using array in this case now
If I have a pizza store that offers say 5 pizza topping options. Then I would gladly use arrays

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

Now let me consider a situation where I am big shot pizza guy and offer close to 60 toppings. So that would mean creating 60 indexes to store the pizza toppings in the array. This causes unnecessary memory usage for not all 60 toppings will be used at a go.

In this case methods seem more appropriate

public Pizza oderPizza (String toppings1){ };
//If I hypothetically call this method from somewhere like this 
class someClass{
order(){ pizza.orderPizza("Pineapple") }
}

Hope that explains. Ask again if you have doubts.