public class list<anytype> {
private node root;
public list(anytype value){ node root=new node(value);
}
public list(anytype [] ary){ this(ary[0]);
for(int i=1;i<ary.length;i++){
this.add(ary[i]);
}
}
         public void add(anytype v){
node temp1=root,temp=root;
while(temp!=null){
temp1=temp;
temp=temp1.next;
}
temp1.next=new node(v);
}
private class node{
anytype value;
node next;
node(anytype v){
value=v;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
list linklist=new list({1,2,3,4,5,6});
linklist.print();
}
}
这样写程序报错,应该是在构造器出现了问题,anytype解释成了int数组而不是int,如果我想用new list({1,2,3,4,5,6})语句调用public list(anytype [] ary)构造器的话,泛型应该怎么写呢? 谢谢各位指导了!

解决方案 »

  1.   

    呵呵
    在下才疏学浅
    没接触过这种写法
    list linklist=new list({1,2,3,4,5,6});
      

  2.   

    其实这里我原来写的是另外一个函数 返回一个int数组,只是为了方便才这样写,debug的时候会直接调用第一个public list(anytype value){ 构造器 而不是第二个
      

  3.   

    本帖最后由 AWUSOFT 于 2011-06-20 01:30:46 编辑
      

  4.   

    泛型不支持基本类型,但是,可以自动拆装箱list<Integer> linklist=new list<Integer>(new Integer[]{1,2,3,4});