You have a few options, either return them in an array, or a Map or use another class to store the values and return that.
/** Use an array to return two value */
public int[] methodReturningTwoInts()
{
int a = getA();
int b = getB();
return new int[] { a, b };
}
/** Use a Map to return two value */
public Map<String, Integer> methodReturningTwoInts()
{
int a = getA();
int b = getB();
Map<String, Integer> result = new HashMap<String, Integer>();
result.put("a", a);
result.put("b", b);
return result;
}
/** Use a object to return two values */
public MyBean methodReturningTwoValues()
{
int a = getA();
String b = getB();
return new MyBean(a, b);
}
written by objects
\\ tags: bean
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MyBean implements Comparable<MyBean>
{
private int value;
public MyBean()
{
}
public MyBean(int value)
{
setValue(value);
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
public int compareTo(MyBean other)
{
return value - other.value;
}
public String toString()
{
return Integer.toString(value);
}
public static void main(String[] args)
{
List<MyBean> listOfMyBeans = new ArrayList<MyBean>();
listOfMyBeans.add(new MyBean(5));
listOfMyBeans.add(new MyBean(1));
listOfMyBeans.add(new MyBean(4));
Collections.<MyBean>sort(listOfMyBeans);
System.out.println(listOfMyBeans);
}
}
written by objects
\\ tags: bean, collection, collections, generics, sort
Recent Comments