I was wondering how to access specific elements of objects in an ArrayList using methods, yet I can't seem to get it to work.
I have a Phone object that has an int price and String color, as well as a method that returns the color.
public class Phone
{
private int price;
private String color;
public String getColor()
{
return color;
}
Now let's say I created an array list of Phone objects called phoneCatalog, and added various Phones. How can I count how many phones are red? This is my attempt that isn't working:
int count = 0;
for(int x = 0; x < phoneCatalog.size(); x++)
if((phoneCatalog.get(x)).getColor.equals("red")
count++;
if((phoneCatalog.get(x)).getColor.equals("red")should beif(phoneCatalog.get(x).getColor().equals("red")right? what do you mean This is my attempt that isn't working? tell us the problem