020-005-006

Collections: Type Safety with Generics

Hard

Problem Description

Collections: Type Safety with Generics

In this problem, you will create a program that stores strings and integers in separate ArrayList<String> and ArrayList<Integer> collections, then displays all elements of the string list and the sum of the integer list to standard output.

Learning Objective: Understand type-safe collection usage with generics

Overview

Generics allow specifying the type of elements stored in collections. Using type parameters enables compile-time type checking, preventing runtime errors.

Specifications

Create a program to manage two lists storing different data types.

  • First input: Number of elements n in string list
  • Next n lines: Strings to add to string list
  • Next input: Number of elements m in integer list
  • Next m inputs: Integer values to add to integer list
  • Output all elements in string list (one element per line)
  • Output sum of all elements in integer list

Input Format

<string count>
<string1>
<string2>
...
<integer count>
<integer1>
<integer2>
...

Output Format

<string1>
<string2>
...
Sum: <sum of integers>

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