Reputation: 11
I'm new to Android Studio and I have been trying to get a matrix from a vector, modify it and put it back but when I increment the matrix, the vector change the automatically its value from where I took the matrix... and I can't figure out why? Here is my code :
Vector<int[][]> internalProces(int prNo, Vector<int[][]> vector)
{
int[][] matrix =new int[processNo][processNo];
matrix=vector.lastElement();
matrix[prNo][prNo]++;
vector.add(matrix);
return vector;
}
public void test(){
Vector<int[][]> vector=new Vector<>();
VectorClk vectorClock = new VectorClk(3);
vector=vectorClock.initialize(vector);
vector=vectorClock.internalProces(1,vector);
}
Thanks!
Upvotes: 1
Views: 35
Reputation: 18235
In matrix=vector.lastElement();
step, you must do a deep copy instead of copy reference:
(from your code I assume that your vector consist of 2-d array with size processNo x processNo
and processNo > prNo
)
int[][] vectorElement = v.lastElement();
for (int i = 0; i< vectorElement.length; i++) {
System.arraycopy(vectorElement[i], 0, matrix[i], 0, vectorElement[i].length);
}
Upvotes: 1