inner loop problem

• 10-08-2011, 01:59 PM
trishtren
inner loop problem
Hello, im trying to iterate through my array to check if all values within the array are the equal, however the inner loop seems to produce 2 results that it should not to begin with, after which it is accurate:
Code:

```public static void Arraychecker(){                 int [] TestArray = { 1, 0 , 3, 4}; // int array with 4 elements                                         for (int i = 0;i < TestArray.length;i++)                 {                                                 //System.out.println("j is"+i);                         for(int j = 0;j < TestArray.length;j++)                         {                                 if (TestArray[i]==TestArray[j++])                                 {                                         System.out.println("i = j");                                 }                         }                 }                 }```
The first test results in i = j being printed twice even when it is not true. Any help solving this would be grand :)
• 10-08-2011, 02:33 PM
jammas615
Re: inner loop problem
You're testing if TestArray[i] is equal to TestArray[j++]. j++ is not a variable but j is.
• 10-08-2011, 02:44 PM
Pojahn_M
Re: inner loop problem
why do you have return 0? Why not put void as return type?
• 10-08-2011, 02:46 PM
trishtren
Re: inner loop problem
good point, but is more or less irrelevant to the problem im trying to solve. thanks anyway.
• 10-08-2011, 02:52 PM
JosAH
Re: inner loop problem
Both i and j run over all index values of your TestArray independently; of course TestArray[0] == TestArray[0] and the same holds for the values 1, 2 and 3 ...

kind regards,

Jos
• 10-08-2011, 03:09 PM
trishtren
Re: inner loop problem
this makes sense, but im trying to compare TestArray[0] against TestArray[1] basically, so i always want to compare 1 value infront of the current one, any idea how i can make this happen correctly?
• 10-08-2011, 03:18 PM
JosAH
Re: inner loop problem
Quote:

Originally Posted by trishtren
this makes sense, but im trying to compare TestArray[0] against TestArray[1] basically, so i always want to compare 1 value infront of the current one, any idea how i can make this happen correctly?

You have to fiddle a bit with the indexes i and j; you want their values to be, (0, 1) (0, 2) (0, 3) (1, 2) (1, 3) (2, 3); that should ring a bell ...

kind regards,

Jos