Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. Infinite loop issue

Hey guys,
It seems that my loop is continuing forever

```A0 = new double[nmax][nmax];
A0_C = new double[nmax][nmax];
A0_C2 = new double[nmax][nmax];
while(!(Arrays.equals(A0_C, A0_C2))){
for (int k = 0; k < A0_C.length; k++)
A0_C[k]=A0_C2[k];
A0_C2 = new double[nmax][nmax];
}
while(!(Arrays.equals(A0, A0_C))){
for (int k = 0; k < A0.length; k++)
A0[k]=A0_C[k];
}```

Any clues??

2. Re: Infinite loop issue

The thing is that in Java what we call 2D arrays are actually arrays of arrays.

That is to say, for your example:

A0_C is an array of reference variables
A0_C[0] is a reference variable that refers to a 1D array of doubles
A0_C[1] is a reference variable that refers to a 1D array of doubles

A0_C2 is an array of reference variables
A0_C2[0] is a reference variable that refers to a 1D array of doubles
A0_C2[1] is a reference variable that refers to a 1D array of doubles

When you execute an statement like
A0_C[k] = A0_C2[k], it sets A0_C[k] to refer to the same array of doubles that is referred to by A0_C2[k] (Doesn't actually copy any doubles from one place to another)

Also, the following important "feature" is what I think your assignment is trying to illustrate:

When you use the Arrays.equals() method on A0_C and A0_C2, it is comparing the reference variables
Is A0_C[0] equal to A0_C2[0] and is A0_C[1] equal to A0_C2[1], etc.

I think the whole point of the exercise is to show that the Arrays.equals() method does not test the contents of the 2D arrays for equality. This is important.

Try to instrument your main() so that you can see what is going on. Maybe like this:
```    public static void main(String [] args) {
int nmax = 2;
double [][] A0 = new double[nmax][nmax];
double [][] A0_C = new double[nmax][nmax];
double [][] A0_C2 = new double[nmax][nmax];
Scanner keyboard = new Scanner(System.in);
while (!(Arrays.equals(A0_C, A0_C2))) {
System.out.println("Top of the first while loop");
System.out.println("1: A0_C = " + Arrays.toString(A0_C));
System.out.println("1: A0_C2= " + Arrays.toString(A0_C2));
System.out.println();

for (int k = 0; k < A0_C.length; k++)  {
A0_C[k]=A0_C2[k];
}
System.out.println("After the for loop");

for (int i = 0; i < nmax; i++) {
System.out.println("2: A0_C [" + i + "] = " + Arrays.toString(A0_C[i]));
System.out.println("2: A0_C2[" + i + "] = " + Arrays.toString(A0_C2[i]));
}

System.out.println("2: A0_C = " + Arrays.toString(A0_C));
System.out.println("2: A0_C2= " + Arrays.toString(A0_C2));
if (Arrays.equals(A0_C, A0_C2)) {
System.out.println("2: A0_C is equal to A0_C2");
}
else {
System.out.println("2: A0_C is not equal to A0_C2");
}

System.out.println();

A0_C2 = new double[nmax][nmax];

System.out.println("After new statement for A0_C2");

for (int i = 0; i < nmax; i++) {
System.out.println("3: A0_C [" + i + "] = " + Arrays.toString(A0_C[i]));
System.out.println("3: A0_C2[" + i + "] = " + Arrays.toString(A0_C2[i]));
}
System.out.println("3: A0_C = " + Arrays.toString(A0_C));
System.out.println("3: A0_C2= " + Arrays.toString(A0_C2));
if (Arrays.equals(A0_C, A0_C2)) {
System.out.println("3: A0_C is equal to A0_C2");
}
else {
System.out.println("3: A0_C is not equal to A0_C2");
}
System.out.println();

System.out.print("Press 'Enter' to continue: ");
keyboard.nextLine();
}

while(!(Arrays.equals(A0, A0_C))){
System.out.println("Top of second while loop");
for (int k = 0; k < A0.length; k++)
A0[k]=A0_C[k];
}
}```

Output from first couple of times through the loop looks like this:
```Top of the first while loop
1: A0_C = [[D@1e0bc08, [D@158b649]
1: A0_C2= [[D@127734f, [D@1037c71]

After the for loop
2: A0_C [0] = [0.0, 0.0]
2: A0_C2[0] = [0.0, 0.0]
2: A0_C [1] = [0.0, 0.0]
2: A0_C2[1] = [0.0, 0.0]
2: A0_C = [[D@127734f, [D@1037c71]
2: A0_C2= [[D@127734f, [D@1037c71]
2: A0_C is equal to A0_C2

After new statement for A0_C2
3: A0_C [0] = [0.0, 0.0]
3: A0_C2[0] = [0.0, 0.0]
3: A0_C [1] = [0.0, 0.0]
3: A0_C2[1] = [0.0, 0.0]
3: A0_C = [[D@127734f, [D@1037c71]
3: A0_C2= [[D@b1c5fa, [D@13caecd]
3: A0_C is not equal to A0_C2

Press 'Enter' to continue:
Top of the first while loop
1: A0_C = [[D@127734f, [D@1037c71]
1: A0_C2= [[D@b1c5fa, [D@13caecd]

After the for loop
2: A0_C [0] = [0.0, 0.0]
2: A0_C2[0] = [0.0, 0.0]
2: A0_C [1] = [0.0, 0.0]
2: A0_C2[1] = [0.0, 0.0]
2: A0_C = [[D@b1c5fa, [D@13caecd]
2: A0_C2= [[D@b1c5fa, [D@13caecd]
2: A0_C is equal to A0_C2

After new statement for A0_C2
3: A0_C [0] = [0.0, 0.0]
3: A0_C2[0] = [0.0, 0.0]
3: A0_C [1] = [0.0, 0.0]
3: A0_C2[1] = [0.0, 0.0]
3: A0_C = [[D@b1c5fa, [D@13caecd]
3: A0_C2= [[D@efd552, [D@19dfbff]
3: A0_C is not equal to A0_C2

Press 'Enter' to continue:```

Try additional tests such as testing Arrays.equals(A0_C[k], A0C2[k]) in a loop, and you will see a valid test for equality.

Another test: comment out the statement inside the first while loop that gets a new value for A0_C2. The first while loop runs once and then the programs goes to the second while loop.

(I think the exercise would have made more sense if the order of the while loops were reversed, but what do I know?)

Bottom line: As this exercise shows, even though the doubles that make up the contents of the 2D arrays are identical, the Arrays.equals() method does not actually test this. If you want to see whether the contents are equal, you have to compare indexed values of the arrays. (That is, use Arrays.equals() on A0_C[k],A0_C2[k] for all of the values of k. If all comparisons show equal, then the array contents are equal. If there is a value of k for which the comparison shows not equal, then the contents are not equal.)

Cheers!

Z

3. The Following User Says Thank You to Zaphod_b For This Useful Post:

Sharmeen (October 21st, 2012)