-4

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];
            }
        }
liwita
  • 1
  • 1
  • 3
    How are `T0` and `T2` declared? In Java, you can't copy an entire array just by assignment: `T0 = T2`. That just copies the *reference* to the array to another variable. So when you change the data for `T0` you are changing the data for `T2`. You need to do a real copy. For reference, you can see [Make copy of array Java](https://stackoverflow.com/questions/5785745/make-copy-of-array-java), for example. Just do a search on "copy array in Java". – lurker Nov 12 '17 at 20:40
  • 2
    Hi. Please remove as much from the code as possible. We appreciate [mcve] here. There are dozens of variables in your code. How shall we know what you're talking about? – Thomas Weller Nov 12 '17 at 20:40
  • 2
    I am downvoting this question because there does not appear to be an effort to debug the issue. Try working through the problem step by step, either by adding print statements in your code to show what the variables hold at each point, using a debugger to trace the program part by part, or even just tracing out the logic on paper. See when things change differently then you expect, and when you find it, ask a specific question, like "Why, when I change T0[2] does T2[2] change at the same time?" (Although that answer can be found in any java tutorial or many existing SO questions) – Davy M Nov 12 '17 at 20:44

1 Answers1

0

Seems you are writing on both T0 and T2 when you assign values to T2. This due to the fact that T0 point to the same array/matrix then T2.

You should deepcopy yhe array instead of doing the assignment. Try to use array.clone() that should create a deep copy of an array of primitives (as T0 and T2 it seem to be).

Stefano Liboni
  • 149
  • 2
  • 11