Reputation: 9543
How can this be done in one line? This is inside a function/method. I am trying to return an array.
float[] t = {
lat, lng
};
return t;
Upvotes: 0
Views: 4169
Reputation: 5452
Either use Jeffrey's idea or you can create a record:
public static class Position{
float lat;
float lng;
public Position(float lat, float lng){
this.lat = lat;
this.lng = lng;
}
}
And then you can pass around a Position:
return new Position(lat, lng)
Jeffrey's is better if you want it for your own application that no one will see and you will use once. This is better if you're publishing this in any way, or if you're using it in multiple methods, called from various places (because it's easier to read).
Also, a record can be better if you're passing it around a lot, because it helps keep track of things. The again, if this is just for one method that's used in one place, it's probably not worth it.
Upvotes: 4