How to write the following code correctly?
public String toString(int[] position, int xOffset, int yOffset) {
String postn = String.format(\"[%d,%d]\", posit
When using Java 8 you could make use of the Pair class.
private static Pair<String, String> foo (/* some params */) {
final String val1 = ""; // some calculation
final String val2 = ""; // some other calculation
return new Pair<>(val1, val2);
}
Otherwise simply return an array.
There are four method which I can think of to implement this scenario:
Create a class with having all the attributes which you want to return, in your case it can be
public class Sample {
private String postn;
private String movm;
public String getPostn() {
return postn;
}
public void setPostn(String postn) {
this.postn = postn;
}
public String getMovm() {
return movm;
}
public void setMovm(String movm) {
this.movm = movm;
}
}
and change your method to include this class's object and set the values in these attributes. Change the return type of the method to Class name e.g. Sample
public Sample toString(int[] position, int xOffset, int yOffset) {
String postn = String.format("[%d,%d]", position[0], position[1]);
String movm = String.format("[%d,%d]", xOffset, yOffset);
Sample obj = new Sample();
obj.setPostn(postn);
obj.setMovm(movm);
return obj;
}
Change the method to return String array and store the values in that array:
public String[] toString(int[] position, int xOffset, int yOffset) {
String postn = String.format("[%d,%d]", position[0], position[1]);
String movm = String.format("[%d,%d]", xOffset, yOffset);
String arr[] = new String[2];
arr[0] = postn;
arr[1] = movm;
return arr;
}
You can read details at link http://www.javatuples.org/
You can use some delimiter if you are sure about the data in the variables. But this will require one spiting with the delimiter, when you want to use these values.
public String toString(int[] position, int xOffset, int yOffset) {
String postn = String.format("[%d,%d]", position[0], position[1]);
String movm = String.format("[%d,%d]", xOffset, yOffset);
return postn+"~"+movm;
}
You cannot return two different values in methods in Java.
When this need occurs, you usually have two options:
Another option would be to use an array of strings, since both your return types are strings. You could use this option as a return value, or a parameter which is altered by the method.
Edit: usually "toString" methods do return only one string. Perhaps you should consider concatenating both your strings into one string using some separator. Example:
return postn + "_" + movm;
In Java, when you want a function to return multiple values, you must
- embed those values in an object you return
- or change an object that is passed to your function ~ dystroy
You have two options (that I know of) here:
public String[] toString(int[] position,int xOffset,int yOffset) {
String postn=String.format("[%d,%d]",position[0],position[1]);
String movm=String.format("[%d,%d]",xOffset,yOffset);
string [] myArray = new string[2];
myArray[0] = postn;
myArray[1] = movm;
return myarray; //returns as array
}
Edited to show get set
private String a;
private String b;
public void setPostN(String s)
{
a= s;
}
public String getPostN()
{
return a;
}
public void setMovm(String s)
{
a= s;
}
public String getMovm()
{
return a;
}
with your method:
public void toString(int[] position,int xOffset,int yOffset) {
String postn=String.format("[%d,%d]",position[0],position[1]);
String movm=String.format("[%d,%d]",xOffset,yOffset);
setPostN(postn);
setMovm(movm);
//no return, but available via calling getMovm() or getPostN()
}
you can either create a wrapper type, instantiate and return it, or return a Map or List/Array.