0

how to find out whether certain elements in an array are equal to one another?
for this array: int array[] = {0, 1, 0, 0, 2, 0, 0, 3, 0, 1}; i wanna know if elements under the indexes 0, 1, and 9 are same numbers

well the easy way is:

if(array[0] == array[1] && array[1]==array[9])
{
    System.out.println("These are same elements!");
}

or something like this, for testing only once

but the thing is, i want to test different set of elements in the array by looping.

any ideas? :?:
thank you :)

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by NormR1
0

You can put it in a method with an array as the parameter.

static void checkArray(int[] array) 
{
   if(array[0] == array[1] && array[1]==array[9])
       System.out.println("These are same elements!");
}

Then you can check all your arrays in a loop.

1

i want to test different set of elements in the array by looping.

Do you know which set of elements you want to check?
In the example you wanted to look at 0,1 and 9.

Are there other sets of specific indexes to check?
Are there always exactly 3 indexes in the set?
Put the indexes in a 2 dim array with each row containing the 3 indexes to be checked.
Then use the contents of that array to check the other array.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.