004-006-003
Array Assignment: Reference Copy
Easy
Problem Description
In this problem, you will create a program that reads integer values from standard input, initializes an integer array, assigns it to another variable, modifies an element of the copy, and displays the first element of both arrays to observe reference copy behavior.
Array Assignment: Reference Copy
Learning Objective: Understand that array assignment copies the reference, not the content
Overview
When you assign an array variable to another variable, the reference (memory location) is copied, not the array content. Therefore, changing one array affects the other.
Input Format
a b c
newValue
- Line 1: Three integers
a,b,c(space-separated) as initial array values - Line 2: An integer
newValueto assign tocopy[0]
Specifications
- Read input with Scanner and initialize integer array
originalwith{a, b, c} - Assign
originalto another array variablecopy - Change
copy[0]tonewValue - Output the first element of both arrays
Output Format
original[0]: newValue
copy[0]: newValue
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