您现在的位置是:主页 > news > 淘宝网站seo服务/网络销售怎么找客户
淘宝网站seo服务/网络销售怎么找客户
admin2025/5/4 10:32:00【news】
简介淘宝网站seo服务,网络销售怎么找客户,广西网络营销外包公司,做外贸网站注意事项问题描述: 学习List的过程中调用list的remove的方法出现了如下的错误: 出现问题的代码 add方法也有相同的问题 OverrideTestpublic void ListTest(){List<Integer> iList Arrays.asList(1, 2, 3);iList.remove(2);}原因分析: 查看A…
淘宝网站seo服务,网络销售怎么找客户,广西网络营销外包公司,做外贸网站注意事项问题描述:
学习List的过程中调用list的remove的方法出现了如下的错误: 出现问题的代码 add方法也有相同的问题
OverrideTestpublic void ListTest(){List<Integer> iList Arrays.asList(1, 2, 3);iList.remove(2);}原因分析:
查看A…
问题描述:
学习List的过程中调用list的remove的方法出现了如下的错误:
出现问题的代码
add方法也有相同的问题
@Override@Testpublic void ListTest(){List<Integer> iList = Arrays.asList(1, 2, 3);iList.remove(2);}
原因分析:
查看Arrays的asList方法:此方法new了一个ArrayList类
public static <T> List<T> asList(T... a) {return new ArrayList<>(a);}
看看asList创造的是怎样的一个ArrayList类
发现了是在Arrays类中的ArrayList内部类(java.util.Arrays$ArrayList)
一般的ArrayList类是java.util.ArrayList
public void ListTest(){List<Integer> iList = Arrays.asList(1, 2, 3);//iList.remove(2);System.out.println(iList.getClass().getName());//java.util.Arrays$ArrayList}//Arrays中的ArrayList类private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable
具体查看remove方法
抽象类AbstractList也就是这两个ArrayList的父类中的remove方法抛出了异常
//抽象类AbstractList也就是这两个ArrayList的父类中的remove方法抛出了异常public E remove(int index) {throw new UnsupportedOperationException();}
java.util.ArrayList能使用remove方法是因为其重写了remove方法
而java.util.Arrays$ArrayList则没有重写
public E remove(int index) {rangeCheck(index);modCount++;E oldValue = elementData(index);int numMoved = size - index - 1;if (numMoved > 0)System.arraycopy(elementData, index+1, elementData, index,numMoved);elementData[--size] = null; // clear to let GC do its workreturn oldValue;}
解决方案:
目前没有其他方案,只能使用util下的ArraysList
@Testpublic void ListTest1(){ArrayList<Integer> iList = new ArrayList<>();System.out.println(iList.getClass().getName());iList.add(1);iList.add(2);iList.add(3);iList.remove(2);}