| Author |
Scroll down to the bottom of the third post for the problem that is currently being asked about.
|
Derek Szpik
Greenhorn
Joined: Feb 20, 2009
Posts: 20
|
|
I have a method:
public MySet union(MySet s){...}
And it joins two sets together, so this method is declared by:
mySet=mySet.union(unionSet);
(alternativly, it could be called with mySet=unionSet.union(mySet);)
I'm implementing the abstract interface MySet using arrays, so each set has an arrElements, which is an array of all of their elements that is created at the same time the sets are initialized.
Inside of the union method, arrElements just refers to the arrElements of mySet, and I need to know how to access the arrElements of unionSet in order to manipulate the values. Does anybody know how I would do this?
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
You have to qualify the access using the reference identifier:
s.arrElements
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Derek Szpik
Greenhorn
Joined: Feb 20, 2009
Posts: 20
|
|
I had tried that, and it didn't work. After hearing the suggestion form somebody else, I decided to play around with it for a bit. MySet is an interface, and doesn't have a constructor, so when I declare 'MySet unionSet' I need to use =new MyArraySet(); since MyArraySet implements MySet. So when the method union required MySet s, there was no arrElements. I had to change it to union(MyArraySet s){...} in order to access the arrElements. Thanks for the confirmation.
Well, remember how MyArraySet needs to implement MySet? By changing union(MySet s) into union(MyArraySet s), MyArraySet no longer implements MySet because the method requires a diferent input. And the real kicker here is that I can't change MySet's union to require an input of MyArraySet because after I implement MySet with arrays, I need to do it with linked lists. This does not make me happy. I'll throw down some code to show what I mean.
This version of MyArraySet implements MySet.
This version of MyArraySet does NOT implement MySet. The only difference is the input of union form MySet to MyArraySet, but it's a big enough change.
[strikethrough]So, the new problem is: how can I get this to work? My idea to be tested at the moment is to change the name of union to something like.. 'sneaky'. THEN, I can make a new method called union, and all that union does is call sneaky. how does that sound? It doesn't solve the spirit of the problem, but it does work around it nicely.[/strikethrough]
(yes, I know there is no strikethrough on this)
So, I just looked at it, and my union is set up SO WELL that I just can't force myself to do ^^. Now, I'm back to the original problem, but with more info, and a clearer question.
I have MySet union(MySet s){...}
When the input MySet is first created, it's done by:
MySet s = new MyArraySet();
What I need to do, is turn the input MySet s into a MyArraySet. How would I do this?
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
You're trying to solve the wrong problem. You need to make MySet into a sufficiently rich interface so that union() can be implemented using methods of the interface. For example, if MySet had a "toArray()" method (and I'm not saying that's the best way to do it, just an easy way!) then union() could be implemented like
Instead of "toArray()", you might have a method "size()" and a method "get(int)" to allow a client to access each element of the set, or (better) implement something analogous to java.util.Iterator, but with int instead of Object.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Derek Szpik
Greenhorn
Joined: Feb 20, 2009
Posts: 20
|
|
OK, I will try that.
Since I have to declare sets as MySet s = new MyArraySet(); I thought that there would be a simple way to turn a set into MyArraySet. I do have a size() method, so I'll try making a get(int) method and working that way, thank you
|
 |
 |
|
|
subject: Scroll down to the bottom of the third post for the problem that is currently being asked about.
|
|
|