115k views
5 votes
Java programming

Create an ArrayList of cereals (Cereal), and add the following cereals to the list:

Frosted Flakes Wheaties Rice Krispies Cheerios Fruit Loops

Print out the list of cereals. Delete Fruit Loops (too sugary). Print out list of cereals. Add Corn Flakes to the list; put it in the second spot. Print out the list of cereals, but ensure you print it out with a tab between each cereal. Finally, print out the size of the ArrayList.

User Luckyape
by
7.3k points

1 Answer

5 votes

Final answer:

To create and manipulate an ArrayList of cereals in Java programming, you need to import the required classes and utilize methods such as add(), remove(), and size().

Step-by-step explanation:

To create an ArrayList of cereals in Java programming, you first need to import the ArrayList class from the java.util package. Then, you can declare and initialize the ArrayList using the Cereal class as the datatype. To add the cereals Frosted Flakes, Wheaties, Rice Krispies, Cheerios, and Fruit Loops to the list, you can use the add() method. To print out the list of cereals, you can iterate through the ArrayList using a for loop and print each cereal using the System.out.println() method. To delete Fruit Loops from the list, you can use the remove() method. To add Corn Flakes to the second spot of the list, you can use the add(index, element) method. To print out the list with a tab between each cereal, you can use the System.out.print() method and concatenate the tab character (\t) with each cereal. Finally, to print out the size of the ArrayList, you can use the size() method.

User Omnisis
by
7.4k points