020-001-005

List Collection: Basic Operations

Easy

Problem Description

List Collection: Basic Operations

In this problem, you will create a class that accepts three fruit names and stores them in an ArrayList. Implement methods to get the list size and retrieve elements by index.

Learning Objective: Learn basic list operations using ArrayList

Overview

List is a collection that stores elements in order. ArrayList is the most commonly used List implementation, supporting add, get, and size operations.

Specifications

  • Constructor takes three String arguments and adds them to an ArrayList
  • getSize(): Returns the list size as int
  • getFruit(int index): Returns the element at the specified index as String

Example

Main basket = new Main("Apple", "Banana", "Cherry");
basket.getSize();    // → 3
basket.getFruit(1);  // → "Banana"

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up