004-002-010

Array Copying with Arrays.copyOf()

Medium

Problem Description

Array Copying with Arrays.copyOf()

In this problem, you will create a program that reads array elements and an expanded size from standard input, uses Arrays.copyOf() to create a same-size copy and an expanded copy, verifies their independence, and displays the results to standard output.

Learning Objective: Understand how to copy and resize arrays using Arrays.copyOf()

Overview

Use Arrays.copyOf() to copy and expand arrays based on input.

Input Format

N
a1 a2 ... aN
M
  • Line 1: Number of elements N
  • Line 2: N space-separated integers
  • Line 3: Expanded copy size M (M > N)

Specifications

  • Create original array from input
  • Copy with same size
  • Copy with expanded size specified by input
  • Verify original and copy are independent

Output Format

Original: a1 a2 ... aN
Copy: a1 a2 ... aN
Expanded: a1 a2 ... aN 0 ... 0
After modifying copy, original: a1 a2 ... aN

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