• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with super in Java

 
Cento e Cem
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Tony Docherty
Bartender
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic