Lets say I have a class called "Sequence". This class has an instance variable private int[] tab
.
There are also some methods to create this tab like those:
public Sequence() {
this.tab = fillAnArray(drawsNumber(5, 20));
}
private int drawsNumber(int min, int max) {
if (min > max) {
throw new IllegalArgumentException("Wrong range");
}
return new Random().nextInt(max - min + 1) + min;
}
public int[] fillAnArray(int size) {
int[] arr = new int[size];
arr[0] = 1;
for (int i = 1; i < arr.length; i++) {
arr[i] = drawsNumber(arr[i - 1], arr[i - 1] + 10);
}
return arr;
}
Now, I would like to create the method that accepts two Sequence objects as arguments and returns number of the same tab elements. So, I created method like this:
public int howManyCommonElements(Sequence c1, Sequence c2) {
int i = 0, j = 0;
int counter = 0;
while (i < c1.tab.length && j < c2.tab.length) {
if (c1.tab[i] == c2.tab[j]) {
++counter;
} else if (c1.tab[i] < c2.tab[j]) {
++i;
} else {
++j;
}
}
return counter;
}
How do I compare single elements like c1.tab[i]
, c2.tab[j]
?