Suppose that we create an array A of GameEntry objects, which has an integer scores field, and we clone A and store the result in an array B. If we then immediately set A[4].score equal to 550, what is the score value of the GameEntry object referenced by B[4]?
class Testarray2{
public static void main(String args[])
{
int A[]={33,3,4,450,120};
A[4]=550;
int B[] = A.clone();
for(int i=0;i<A.length;i++)
System.out.println(A[i]);
for(int i=0;i<B.length;i++)
System.out.println(B[i]);
}
}
The game entry object referenced by B[4] will updated with 550
Well, I think the question states that, if we have an array A and then take another array B which is a clone of A. If we then change the value of A[n], what will be the value referenced by B[n]. The answer is the original A[n] before it was changed.
public class TestArray2 {
public static void main(String[] args) {
int A[] = {1, 2, 3, 4, 5};
int B[] = A.clone();
A[4] = 44;
System.out.println(B[4]); // 5
}
}
Post the discussion to improve the above solution.