Re: Java type-casting -- Q3
grz01 wrote:
On 26 Sep, 00:45, markspace <nos...@nowhere.com> wrote:
... The code you wrote made my eyes bleed ...
OK, Mark, since I dont want to
risk anyones physical wellbeing :)
I'll re-phrase the question
(starting from scratch with a new thread).
The problem I started out with was from a real programming task.
I wanted to make a static function
that returns a Pair of Lists,
where the first component
(lets say, for simplicity)
is known to be: List<Integer>
and the other component is: List<String>
So the return-type really should be:
Pair<List<Integer>,List<String>>
But since I dont think Java has any type for Pair<A,B> (?) and I dont
like to create yet another custom-class only for this trivial purpose,
It seems simpler and more straightforward to me
-------------------------8<----------------------------------
import java.util.ArrayList;
import java.util.List;
public class PairOfLists {
public static void main(String[] args) {
System.out.println(doSomething().getB().get(0));
}
public static Pair<List<Integer>, List<String>> doSomething() {
List<Integer> m = new ArrayList<Integer>();
m.add(42);
List<String> n = new ArrayList<String>();
n.add("Slartibartfast");
return new Pair<List<Integer>,List<String>>(m,n);
}
}
class Pair<A,B> {
A a;
B b;
Pair(A a, B b) {
this.a = a;
this.b = b;
}
public A getA() { return a; }
public B getB() { return b; }
}
-------------------------8<----------------------------------
Compiles cleanly, needs no nasty casts, the generics are easy to follow,
When run produces the output I'd expect.
--
RGB