I am trying to use a variable from the previous iteration in a do-while loop to make a calculation in the current iteration, and then use it for a convergence test at the end of the loop.
Here's the code (java):
int z = 0;
do {
T0 = T2;
for (int j = 0; j < K; j++) {
for (int i = N - 1; i >= 0; i--) {
if (i == 0) {
T1[i][j] = T0[i + 1][j] + C[i]; // When x=0, c=0
}
if (i > 0 && i < N - 1) {
T1[i][j] = T0[i + 1][j] + T0[i - 1][j]
+ C[i];
}
if (i == N - 1) {
T1[i][j] = T0[i - 1][j] + C[i]; // When x=W, b=0
}
}
}
for (int i = 0; i < N; i++) {
for (int j = K - 1; j >= 0; j--) {
if (j == 0) {
T2[i][j] = T1[i][j + 1] + C[j]; // When y=0, c=0
}
if (j > 0 && j < K - 1) {
T2[i][j] = T1[i][j + 1] + T1[i][j - 1]
+ C[j];
}
if (j == K - 1) {
T2[i][j] = 273.15;
}
}
}
z++;
} while (z < 5);
I'm attempting to set T0 = T2 from the previous iteration at the beginning of the loop, and T0 will then be compared to T2 from the current iteration at the end of the loop for a convergence test (not included here). At the beginning of the loop, T0 is in fact equal to T2 from the previous iteration. However at the end of the loop, T0 is equal to T2 from the current iteration. So how can I keep T0 from changing from the beginning to the end of the loop within an iteration? I'm new to java, and any help/comments are much appreciated!
UPDATE: Thanks for the quick feedback. Both System.arraycopy() and array.clone() seems to do the job for a 1D array. I Might be missing something, but it seemed a bit more challenging for copying 2D matrices? But for my purpose, this also worked:
for (int i=0; i<N;i++) {
for (int j=0; j<K;j++) {
T0[i][j]=T2[i][j];
}
}