4.7k views
3 votes
JAVA

Given an array of Clothes objects, return an ArrayList of objects that have a color of "blue".

public ArrayList onlyBlue(Clothes[] clothes){

}

User Foufrix
by
4.8k points

1 Answer

1 vote

Answer:

public ArrayList onlyBlue(String[] clothes){

ArrayList<String> blueCloths = new ArrayList<>();

for(int i =0; i<clothes.length; i++){

if(clothes[i].equalsIgnoreCase("blue")){

blueCloths.add(clothes[i]);

}

}

return blueCloths;

}

Step-by-step explanation:

  • Create the method to accept an Array object of type String representing colors with a return type of an ArrayList
  • Within the method body, create and initialize an Arraylist
  • Use a for loop to iterate the Array of cloths.
  • Use an if statement within the for loop to check if item equals blue and add to the Arraylist.
  • Finally return the arrayList to the caller
User Nulll
by
4.8k points