how to deal with multiple return values
Author |
Message |
bugzpodder
![](http://www.vbforums.com/avatar.php?userid=31755&dateline=1038631511)
|
Posted: Mon Mar 13, 2006 11:33 am Post subject: how to deal with multiple return values |
|
|
I need to return two things in Java: an int and an Object of some class.
In C++ it would be easy since we could have reference parameters. The int is to indicate the method status. This is almost an exception, except that regardless of method status, I still want the whole method executed!
There are a couple of ways I think think of, all of which seem to be very complicated. What is the best way?
1) Create a new exception class, throw exceptions for each status at the very end, and add the object to the exception
2) Create a new class that is basically container for a int and the Object and change return type to this new class
3) return a List<Object> that returns the first element to be the Object and second to Integer
4) Have two methods that does essentially the same thing. Except one computes the integer and the other computes the Object. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Mon Mar 13, 2006 11:51 am Post subject: (No subject) |
|
|
Why not explain why you feel the need to have a method set up this way? |
|
|
|
|
![](images/spacer.gif) |
bugzpodder
![](http://www.vbforums.com/avatar.php?userid=31755&dateline=1038631511)
|
Posted: Mon Mar 13, 2006 12:31 pm Post subject: (No subject) |
|
|
I have a method extractVertex which takes arguments: Vertex, Edge, and a ruleset which basically tells me if an edge is valid or not. The Vertex is one end of the Edge. Obviously this method could be used in a lot of places.
So there are three interesting pieces of information: The vertex on the other end of the edge, as well as whether the ruleset defined the edge to be valid or not, and finally the direction in the directed edge that we are travelling. I would think that the most plausible way is to define a new class. But efficiency is a concern as it seems that system grinds and churns even on O(nlogn) algorithms. Right now I hacked it so it returns null whenever we don't like the edge. ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Mon Mar 13, 2006 12:38 pm Post subject: (No subject) |
|
|
Sounds valid, However, you may wish to consider having the check to see if it's valid (if I understand you correctly) happen in a separate method.
Would it be out of line to ask to see your code? Not sure it'll do me any good, but I may be able to discern something from it. |
|
|
|
|
![](images/spacer.gif) |
Justin_
|
Posted: Mon Mar 13, 2006 1:00 pm Post subject: (No subject) |
|
|
There are no references in Java?? |
|
|
|
|
![](images/spacer.gif) |
rizzix
|
Posted: Mon Mar 13, 2006 3:37 pm Post subject: (No subject) |
|
|
Alas bugz, java does not have Tuples -- a wanted feature, that hasen't been implemented yet.
I would suggest you create a Pair class, if you're sure you are only going to return just two values and nothing more. (You technically cannot create a type-safe Tuple class -- for obvious reasons)
Java: | public class Pair<T,K> {
T valueA = null;
K valueB = null;
public Pair() {}
public Pair(T a, K b) {
this.valueA = a;
this.valueB = b;
}
public void setFirst(T a) {
valueA = a;
}
public T getFirst() {
return valueA;
}
public void setSecond(K b) {
valueB = b;
}
public K getSecond() {
return valueB;
}
} |
|
|
|
|
|
![](images/spacer.gif) |
bugzpodder
![](http://www.vbforums.com/avatar.php?userid=31755&dateline=1038631511)
|
Posted: Mon Mar 13, 2006 3:37 pm Post subject: (No subject) |
|
|
actually... I was explaining how the checking of the edge works, but decided to delete the explianation. In short, ruleset requires the traversal direction, which is why I put it in the same method.
I guess some code doesn't hurt, esp since I wrote it.
If you are curious, Vertex is basically a wrapper for an Object, and Edge is part of jgrapht-0.6.0 library.
code: |
Object v1 = edge.getSource();
Object v2 = edge.getTarget();
TraverseType type;
Vertex neighbour;
if (v2 == vert)
{
neighbour = (Vertex) v1;
type = TraverseType.BACKWARD;
}
else
{
neighbour = (Vertex) v2;
type = TraverseType.FORWARD;
}
//If rules are broken, return null
if (!rules.useVertex(neighbour.getData()) || !rules.useEdge(vert.getData(), neighbour.getData(), type))
return null;
return neighbour;
|
This is part of the code for what I call a BinaryGraph. this is probably the first new (AFAIK) data structure I've came up with.
In short, in an unweighted graph, it supports finding the shortest path in linear time (nothing new, which is just a BFS), with the following improvements:
ruleset to disable edges/vertices on the fly without modifying graph (trivial, three lines, one is shown above)
Notion of grouping vertices and or other groups (permanently or temporarily - ie easily removable). If two vertices are in a group then there's an edge between them.
If a group has k elements, then it takes O(k) space and O(k) BFS search time (assuming they all get visited on the first try -- if they didn't, that means there was a rule in a ruleset that disabled an edge, and thus in the worst case will take O(k^2) time).
Notice that a naive implementation of adding all the k^2 edges takes O(k^2) space and time regardless
Thats my two cents to the CS Field
Writing my work report on it. ![Smile Smile](images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Mon Mar 13, 2006 3:49 pm Post subject: (No subject) |
|
|
Justin_ wrote: There are no references in Java??
All objects in Java are passed by reference. It is best not to abuse this, though, especially as Java offers no way to counter this behavior. It makes discipline especially important. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
bugzpodder
![](http://www.vbforums.com/avatar.php?userid=31755&dateline=1038631511)
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Mon Mar 13, 2006 4:02 pm Post subject: (No subject) |
|
|
Heh. I would have provided that explantion, but I doubted Justin_'s familiarity with references that work the other way (I had things like Ada in mind). Considering he's mentioned looking at C++, that may have been a mistake on my part. |
|
|
|
|
![](images/spacer.gif) |
Justin_
|
Posted: Wed Mar 15, 2006 1:27 am Post subject: (No subject) |
|
|
Very interesting, thanks for link. |
|
|
|
|
![](images/spacer.gif) |
|
|