This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
Please use Java naming conventions ie methods and variables start with lowercase letters.
because the method "RemoveDups" must be static
to be passed as a parameter of super()
You aren't passing RemoveDups as a parameter to super(), you are passing the value returned by RemoveDups.
I haven't time to look at your code and fully understand what you are trying to do but something feels wrong about the design here. However a simple way around your problem would be to have a static method that just counts and returns the number of unique values in the array and use that to pass the size to super() and then have RemoveDups() as an instance method which you call after calling super().