由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 请问有没有generic的array
相关主题
error: generic array creation问个primitive type的问题
Java里面有没有可能写个带generic parameter的class对built-in type也适用?看了zhaoce073大水忍不住说2句
问两个语法问题问一个Java的问题,关于create generic array
extending generic class , but not mentioning its parameterized type?Re: 初级问题
如何造Array of Generic Typehow to copy an Object?
generics这样改对马?问一个generic的问题吧
An interesting thing about java generics-do not laugh at me if u think it too basic[转载] Java 1.5 Generic 问题
java 的函数 xxx(a, b,c)能够向a 写入数据吗?Question: reflection and generics
相关话题的讨论汇总
话题: foo话题: generics话题: array话题: generic话题: lala
进入Java版参与讨论
1 (共1页)
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?谢谢!

1 (共1页)
进入Java版参与讨论
相关主题
Question: reflection and generics如何造Array of Generic Type
Is it possible to get Class object for T from a generic class? (下列空档,是否可填)generics这样改对马?
一个关于generics的问题An interesting thing about java generics-do not laugh at me if u think it too basic
Interview的人问我最新java 版本是多少java 的函数 xxx(a, b,c)能够向a 写入数据吗?
error: generic array creation问个primitive type的问题
Java里面有没有可能写个带generic parameter的class对built-in type也适用?看了zhaoce073大水忍不住说2句
问两个语法问题问一个Java的问题,关于create generic array
extending generic class , but not mentioning its parameterized type?Re: 初级问题
相关话题的讨论汇总
话题: foo话题: generics话题: array话题: generic话题: lala