Hi folks:
I have a problem with a tiny
Java program.
The problem is:
Giving an array of ints (with possible duplicate elements) I want to list pairs of numbers in ascending order without repetitions.
The classes "Sequence" and "FixedSize" can't be changed, because they are inherited by other classes (external to this code) and they ran without errors.
The only class that may (and need to) be changed is "SortArray".
Here's the code:
When we ran the code snippet:
The expected output should be:
0:1 - 1:2 - 2:5 - 3:8 - - 0:2 - 1:5 (two sequences of numbers - one for each while - in ascending order without repetitions)
However this code doesn't run, because the static array "WithoutRepetitions[]" being static is shared by the two instances of Sequence (s1 and s2),
and both while loops show the second sequence (0:2 - 1:5) crashing the program in the first while because the length of s1 is bigger than the length of s2.
If I
exchange the order of instructions like below, it works:
In that order it works, however I'm not allowded to do this:
Somebody advice me to instead of a static array, I must use an instance array, however this is not possible, because the method "RemoveDups" must be static
to be passed as a parameter of super() and because of this constraint (static) can't access non static variables.
Can someone help me to solve this ambiguity? This is a kind of "deadlock" problem I can't solve.
Thank you in advance for your help.