Suppose you declare a static array of integers, say, int arr = [5]. (10 points)
ID: 3904244 • Letter: S
Question
Suppose you declare a static array of integers, say, int arr = [5]. (10 points)
You initialize its values. Then, you declare two more arrays:
one that is also static, called sArr, and another dynamic, called dArr.
a. Explain the process of initializing dArr by shallow-copying arr.
b. Explain the process of initializing sArr by deep-copying arr.
à
c. Explain what methods you use (loops, temporary variables, pointer arithmetic, etc.)
d. Now, suppose you change one or more of the elements in dArr.
What will happen to the corresponding elements in sArr?
What about in arr?
Explanation / Answer
Answer1
Process of intializing dArr the Arr by shallow copying arr
A shallow copy means simply copying the reference
dArr=Arr
Answer 2
Process of intializing sArr by deep copying arr
a deep copying means creating a new array and copying the values to that array.
sArr=new int[arr.length]
for(int i=0;i<sArr.length;i++)
{
sArr[i]=arr[i];
}
Answer 3
here we used for loop to iterate through and copy the elements to sArr. and we used assaignment operator(=)
Answer 4
If we change one or more elements in dArr it won't effect on sArr because sArr is implemented by deep ccopying.
if we change in dArr it will impact on arr because both references pointed to same member
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.