Collection这个接口只能用子类实例化,又没有get()方法(子接口List中才有)。实例化了,添加了元素我怎么取出来呢,比如:Collection<String> col=new ArrayList<String>()取个元素,更改个元素好麻烦。

解决方案 »

  1.   

    Collection<String> col=new ArrayList<String>()
    for(String str : col.iterator()){
       
       //
    }
      

  2.   

    因为Set不需要get方法,所以不能从List提升到Collection
      

  3.   

    迭代是个不错方法,但如果我想像List一样取出(更改)指定索引位置的元素,怎么办呢?必须用List么?为什么Set不需要?我向里面添加了数据,我都想方便的从里面取出来(或更改某一索引位置的元素,像数组一样)
      

  4.   

    为了满足这样的需求,我弄了个String的数组,把里面的元素一个一个放进去,改完了,又把数组转成List再换给他,麻烦。有什么简便方法么?public void update(int x,String t)
    {
    String[] s=new String[this.getColl().size()];
    Iterator<String> it=this.getColl().iterator();
    while(it.hasNext())
    {
    for(int i=0;i<s.length;i++)
    {
    s[i]=it.next();
    }
    }
    s[x]=t;
    this.cle=Arrays.asList(s);
    }
      

  5.   

    那你直接List<String> col=new ArrayList<String>();
    不就得了
      

  6.   


    是,直接用List早就不用纠结了,我就没想明白,为什么非得这样设计。
      

  7.   

    如果你认为这个this.getColl()应该存在get方法,那说明它应该是一个List