• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with super in Java

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic