-31-
12
2009
12
2009
Mehrere Werte in Java-Methoden typsicher zurückgeben
Trackbacks
Trackback für spezifische URI dieses Eintrags
Keine Trackbacks
private Object[] doItUnchecked()
{
String s = "foo";
Date d = new Date();
return new Object[] { s, d };
}
// unchecked variant: dangerous! Object[] returnValues = t.doItUnchecked(); String s = (String) returnValues[0]; Date d = (Date) returnValues[1];
/**
* Immutable holder type for two values.
*
* @author Benjamin Erb
*
* @param <F> type of first value
* @param <S> type of second value
*/
public class PairHolder<F, S>
{
private final F first;
private final S second;
public PairHolder(F first, S second)
{
this.first = first;
this.second = second;
}
public F getFirst()
{
return first;
}
public S getSecond()
{
return second;
}
}
private PairHolder<String, Date> doItChecked()
{
String s = "foo";
Date d = new Date();
return new PairHolder<String, Date>(s, d);
}
// check variant: safe already at compile-time PairHolder<String, Date> h = t.doItChecked(); String s = h.getFirst(); Date d = h.getSecond();
|
|
September '10 | |||||
| Mo | Di | Mi | Do | Fr | Sa | So |
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | |||
© 2002 - 2010 Benjamin Erb