# Thread: inner loop problem

1. Member
Join Date
Feb 2011
Posts
53
Rep Power
0

## 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:
Java 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 :)
Last edited by trishtren; 10-08-2011 at 02:51 PM.

2. Senior Member
Join Date
Jul 2011
Location
Melbourne, Victoria, Australia
Posts
155
Rep Power
6

## Re: inner loop problem

You're testing if TestArray[i] is equal to TestArray[j++]. j++ is not a variable but j is.

3. ## Re: inner loop problem

why do you have return 0? Why not put void as return type?

4. Member
Join Date
Feb 2011
Posts
53
Rep Power
0

## Re: inner loop problem

good point, but is more or less irrelevant to the problem im trying to solve. thanks anyway.

5. ## 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

6. Member
Join Date
Feb 2011
Posts
53
Rep Power
0

## 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?

7. ## Re: inner loop problem

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•