java.util.RandomAccessSubList cannot be cast to java.util.ArrayList解决办法


public class subListTest { public static void main(String[] args){ ArrayList list = new ArrayList(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); List listsub = list.subList(0, 3); System.out.println(listsub); } }

  List listsub = list.subList(0, 3); 如果改成下面的式子

     ArrayList listsub = list.subList(0, 3);就会报标题的java.util.RandomAccessSubList cannot be cast to java.util.ArrayList错误。

原因分析是:subList返回的是list,问题就是在subList方法中

public List subList(int fromIndex, int toIndex) {
        return (this instanceof RandomAccess ?
                new RandomAccessSubList(this, fromIndex, toIndex) :
                new SubList(this, fromIndex, toIndex));
    }

  

 

转载于:https://www.cnblogs.com/yuwenfeng/archive/2013/05/31/3110694.html

你可能感兴趣的:(java.util.RandomAccessSubList cannot be cast to java.util.ArrayList解决办法)