g****y 发帖数: 436 | 1 foo(int[][] lala);
foo2(Double[][] lala);
现在想只保留一个generic的fooG。
google半天,没有结果。。
请问有没有这种generic array?谢谢! |
l********0 发帖数: 283 | 2 Object lala
可以包容一切,在函数里面再判断数据类型
【在 g****y 的大作中提到】 : foo(int[][] lala); : foo2(Double[][] lala); : 现在想只保留一个generic的fooG。 : google半天,没有结果。。 : 请问有没有这种generic array?谢谢!
|
g****y 发帖数: 436 | 3 谢谢,我开始也是这样想的。但是后来我想,如果我expect一个int[][],结果来了一个
List[][],事情就不好办了。如果
void foo(Object[][] lala){
for(Object[] la : lala){
for(Object l : la){
System.out.println(l.toString());
}
}
}
我怎么才能在foo里面filter掉像List这样的输入呢? 不会是来一大堆 if吧。
【在 l********0 的大作中提到】 : Object lala : 可以包容一切,在函数里面再判断数据类型
|
Z****e 发帖数: 2999 | 4 try using
void foo(T[][] lalal)
一个
【在 g****y 的大作中提到】 : 谢谢,我开始也是这样想的。但是后来我想,如果我expect一个int[][],结果来了一个 : List[][],事情就不好办了。如果 : void foo(Object[][] lala){ : : for(Object[] la : lala){ : for(Object l : la){ : System.out.println(l.toString()); : } : } : }
|
g****y 发帖数: 436 | 5 thanks,原来hardware大侠也在这里啊。哈哈
【在 Z****e 的大作中提到】 : try using : void foo(T[][] lalal) : : 一个
|
c*****t 发帖数: 1879 | 6 There is no way to have generic T[][] that references to int[][].
Has to be Integer[][].
static void foo (T[][] array)
{
}
If you want strictly int[][] (or other 2D primitive arrays), usually
it is better to write multiple versions for each one, since ultimately
you would have to access each members through specific code and generics
doesn't help.
【在 g****y 的大作中提到】 : foo(int[][] lala); : foo2(Double[][] lala); : 现在想只保留一个generic的fooG。 : google半天,没有结果。。 : 请问有没有这种generic array?谢谢!
|
g****y 发帖数: 436 | 7 谢谢!
我还是第一次看到!实在是太巧妙了!!!!解决了那个List
o>的问题!
不过为什么要加一个static在这里呢?
【在 c*****t 的大作中提到】 : There is no way to have generic T[][] that references to int[][]. : Has to be Integer[][]. : static void foo (T[][] array) : { : } : If you want strictly int[][] (or other 2D primitive arrays), usually : it is better to write multiple versions for each one, since ultimately : you would have to access each members through specific code and generics : doesn't help.
|
Z****e 发帖数: 2999 | 8 there's auto-boxing, but it only works on one dim array.
interesting thing is that in a function with generics, you can use T to do
typecast, but you cannot do things like T.class
【在 c*****t 的大作中提到】 : There is no way to have generic T[][] that references to int[][]. : Has to be Integer[][]. : static void foo (T[][] array) : { : } : If you want strictly int[][] (or other 2D primitive arrays), usually : it is better to write multiple versions for each one, since ultimately : you would have to access each members through specific code and generics : doesn't help.
|
c*****t 发帖数: 1879 | 9 我自己写 test 的时候随手弄得。俺对这 generics 也不是很熟悉,只是
印象中见过可以 specify T 的 subclass,所以得测试一下。
fo
【在 g****y 的大作中提到】 : 谢谢! : 我还是第一次看到!实在是太巧妙了!!!!解决了那个List: o>的问题! : 不过为什么要加一个static在这里呢?
|
g****y 发帖数: 436 | 10 呵呵,测试了一下,eclipse报错,如果用List的话。太感谢了!
【在 c*****t 的大作中提到】 : 我自己写 test 的时候随手弄得。俺对这 generics 也不是很熟悉,只是 : 印象中见过可以 specify T 的 subclass,所以得测试一下。 : : fo
|
s******n 发帖数: 876 | 11 can't you do
foo(Number[][] la)
【在 g****y 的大作中提到】 : foo(int[][] lala); : foo2(Double[][] lala); : 现在想只保留一个generic的fooG。 : google半天,没有结果。。 : 请问有没有这种generic array?谢谢!
|
b******y 发帖数: 9224 | 12
Why are you making things complicated?
I think for software development, keep it simple is the key. Too much
generalization is not good.
1) performance hurts if you use objects. Remember that saying, there is no
free lunch. Use primitives if you can.
2) what's wrong with having two methods? I think you have more to worry
about than trying to over-engineering the thing.
Anyway, personal opinion, don't take my word as hostile, hehe.
【在 g****y 的大作中提到】 : foo(int[][] lala); : foo2(Double[][] lala); : 现在想只保留一个generic的fooG。 : google半天,没有结果。。 : 请问有没有这种generic array?谢谢!
|
g****y 发帖数: 436 | 13 thanks. actually i agree what you said, however, in my project, we have to d
eal with various heterogenious data, which requires a relatively immutable f
ramework, so that we wont need to modify our code whenever we get new data.
【在 b******y 的大作中提到】 : : Why are you making things complicated? : I think for software development, keep it simple is the key. Too much : generalization is not good. : 1) performance hurts if you use objects. Remember that saying, there is no : free lunch. Use primitives if you can. : 2) what's wrong with having two methods? I think you have more to worry : about than trying to over-engineering the thing. : Anyway, personal opinion, don't take my word as hostile, hehe.
|
F****n 发帖数: 3271 | 14 Generics is not for primitive data types. There's no Generics for primitive
array.
【在 g****y 的大作中提到】 : foo(int[][] lala); : foo2(Double[][] lala); : 现在想只保留一个generic的fooG。 : google半天,没有结果。。 : 请问有没有这种generic array?谢谢!
|