数组转集合新颖写法get√

作者: 青楼爱小生 | 来源:发表于2018-03-30 08:28 被阅读75次

前言:啥也不说了,show me the code!

static List<Integer> intArrayAsList(final int[] a){
    if(a == null)
        throw new NullPointerException();

    retrun new AbstractList<Integer>() {
        public Interger get(int i) {
            retrun a[i];
        }
        @Override public Integer set(int i, Integer val){
            int oldVal = a[i];
            a[i] = val;
            retrun oldVal;
        }

        public int size(){ retrun a.length;}
    };
}

相关文章

网友评论

  • JarryLeo:没有实现 add remove 无法扩容。并不算集合吧
  • ZHTF:Arrays.tolist

本文标题:数组转集合新颖写法get√

本文链接:https://www.haomeiwen.com/subject/cxsbcftx.html