Reputation: 730
is it possible for the following element to return multiple items in one call ( i.e. two GRects)
private GObject getColidingObject(){
if(getElementAt(ball.getX(), ball.getY()) != null){
return getElementAt(ball.getX(), ball.getY());
}else if(getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY()) != null){
return getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY());
}else if(getElementAt(ball.getX(), ball.getY() + BALL_RADIUS *2) != null){
return getElementAt(ball.getX(), ball.getY() + BALL_RADIUS *2);
}else if(getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS *2) != null){
return getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS *2);
}else{
return null;
}
}
Upvotes: 0
Views: 5205
Reputation: 19613
The simple answer is "No", you can only have one return value.
But the majority of the posters are right in saying you can use collections to return multiple things. I notice that most everyone seems to advocate returning either a list or a collection object, which ceratinly works, but if your problem is isolated to within a particular class you're developing, you might also consider just defining a convience inner class to make your life easier. This is an absolutely brain-dead example,but hopefully it gets the point across:
public class InnerClassDemo
{
public static void main(final String[] args)
{
InnerClassDemo icd = new InnerClassDemo();
Holder h = icd.getTwoRandomValues();
System.out.println("Value 1 = " + h.getVal1());
System.out.println("Value 2 = " + h.getVal2());
}
public Holder getTwoRandomValues()
{
Random r = new Random();
int x = r.nextInt();
int y = r.nextInt();
return(new Holder(x,y));
}
private class Holder
{
private int val1;
private int val2;
public Holder()
{
this.val1 = 0;
this.val2 = 0;
}
public Holder(int val1,int val2)
{
this.val1 = val1;
this.val2 = val2;
}
public int getVal1()
{
return this.val1;
}
public void setVal1(int val1)
{
this.val1 = val1;
}
public int getVal2()
{
return this.val2;
}
public void setVal2(int val2)
{
this.val2 = val2;
}
}
}
So in your case, just make an inner class that holds a couple GRects if passing around two GRects at a time is going to be a common operation for you.
Upvotes: 0
Reputation: 396
Yes, but this is going to be a nightmare for someone to maintain in a production environment.
Upvotes: 0
Reputation: 189484
If you want to return more then one item of the same type you always can return a collection of this objects. If you will always return two objects an array may be better because it gives you a fast sorted access to the objects.
If you have many values and need to return them you maybe need to introduce a new kind of object to store all the values in and return this object then. It is a very good hint that you sooner or later need this object to move the data around in you application if you encounter many arrays, lists of Intergers or pairs.
Upvotes: 0
Reputation: 515
Yes and no. The short answer is not the way you're trying to do it there.
If you want to return "two" things, you can encapsulate them into one object that can be returned as a single entity.
eg, a Map or any other collection should do the trick. Alternative, you could return an array.
Upvotes: 1
Reputation: 199234
No it is not.
A Java function has only one return value.
If you really need to return several values, you may return either a list or an array of the values like this:
private List<GObject> getColidingObject() {
List<GObject> results = new ArrayList<GObject>();
if( cond1() ) {
results.add( getElementAt( .... ) ;
} else if( cond2() ) {
results.add( getElementAt( .... ) ;
} else if ( etc ...
return results;
}
Upvotes: 0
Reputation: 7480
Like a collection of GObject?
private List<GObject>getColidingObject(){
...
List<GObject> colidingObjects = new ArrayList<GObject>();
colidingObjects.add(...);
return colidingObjects;
}
Upvotes: 2
Reputation: 1501013
You can only return one value, but you could make that value an array. For example:
private GObject[] getCollidingObjects() {
GObject[] ret = new GObject[2];
ret[0] = ...;
ret[1] = ...;
return ret;
}
Btw, when you start reusing the same expression multiple times in the same method, you should think about introducing a local variable for clarity. For example, consider this instead of your original code:
private GObject getCollidingObject(){
int x = ball.getX();
int y = ball.getY();
if (getElementAt(x, y) != null) {
return getElementAt(x, y);
}
if (getElementAt(x + BALL_RADIUS * 2, y) != null) {
return getElementAt(x + BALL_RADIUS * 2, y);
}
if (getElementAt(x, y + BALL_RADIUS * 2) != null) {
return getElementAt(x, y + BALL_RADIUS * 2);
}
if (getElementAt(x + BALL_RADIUS * 2, y + BALL_RADIUS * 2) != null) {
return getElementAt(x + BALL_RADIUS * 2, y + BALL_RADIUS * 2);
}
return null;
}
(You could do the same for x + BALL_RADIUS * 2
and y + BALL_RADIUS * 2
as well.)
You might also consider something like this:
private GObject getCollidingObject(){
int x = ball.getX();
int y = ball.getY();
return getFirstNonNull(getElementAt(x, y),
getElementAt(x + BALL_RADIUS * 2, y),
getElementAt(x, y + BALL_RADIUS * 2),
getElementAt(x + BALL_RADIUS * 2, y + BALL_RADIUS * 2));
}
private static getFirstNonNull(GObject... objects) {
for (GObject x : objects) {
if (x != null) {
return x;
}
}
return null;
}
(In C# there's a nicer way of doing this with the null coalescing operator, but never mind...)
Upvotes: 9
Reputation: 89085
It will always return one object. That object, however, may be a container class inheriting from GObject that holds multiple objects, e.g. a GCompound.
Upvotes: 0
Reputation: 41158
A method can only have 1 return. As soon as a return is found, the method finishes it's execution.
Upvotes: -2
Reputation: 21369
You could return an array or a list of GRects. Or, perhaps supply an empty array or list that the method could fill.
Upvotes: 1
Reputation: 5488
No it is only possible to return one item from the call. In the code you posted it only one of the getElementAt lines will execute.
Upvotes: 4
Reputation: 3349
Nope. Unless there's a GArray. But as written, no. It will return one or "zero" (null) object.
Upvotes: 0
Reputation: 774
Are you asking if your function could return multiple objects?
I think the simplest thing to do would be to return a List or array of objects in your function.
Upvotes: 3