[Java错误]使用Collections中的copy方法复制ArrayList产生的错误
来源:互联网 发布:mac下的思维导图 编辑:程序博客网 时间:2024/06/05 10:37
错误信息如下:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Source does not fit in dest
at java.util.Collections.copy(Unknown Source)
错误源代码:
import java.util.ArrayList;import java.util.Collections;public class test_4 {public static void main(String args[]) {ArrayList<Integer> List = new ArrayList<Integer>();for (int i = 0; i < 10; i++)List.add(i); ArrayList<Integer> newList = new ArrayList<Integer>(100);Collections.copy(newList, List);System.out.println(List.toString());System.out.println(List);System.out.println(newList);List.set(2, 8);System.out.println(newList);}}
看下API文档中对于copy方法的介绍如下:
public static <T> void copy(List<? super T> dest, List<? extends T> src)
Copies all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list. The destination list must be at least as long as the source list. If it is longer, the remaining elements in the destination list are unaffected.
This method runs in linear time.
- Type Parameters:
T
- the class of the objects in the lists- Parameters:
dest
- The destination list.src
- The source list.- Throws:
IndexOutOfBoundsException
- if the destination list is too small to contain the entire source List.UnsupportedOperationException
- if the destination list's list-iterator does not support the set operation.- 错误的方式打印出来的newList.size()==0!!!可是我们明明将其设置为了100!Why?通过仔细观察,发现100并不是指的是newList的size,而是其容量(打个比方,你有一个桶,它的容量很大,但是它是空的!!!)。笔者认识到该错误后,对源码进行了修改,得到了正确的方式,修改如下:
import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;public class test_4 {public static void main(String args[]) {ArrayList<Integer> List = new ArrayList<Integer>();for (int i = 0; i < 10; i++)List.add(i); ArrayList<Integer> newList = new ArrayList<Integer>(Arrays.asList(new Integer[List.size()]));Collections.copy(newList, List);System.out.println(List.toString());System.out.println(List);System.out.println(newList);List.set(2, 8);System.out.println(newList);}}
0 0
- [Java错误]使用Collections中的copy方法复制ArrayList产生的错误
- java Collections.copy的使用
- Collections的copy()方法和ArrayList的大小问题
- 由java中深度复制一伸出Collections.copy的使用
- Collections.copy的使用
- java 之 Collections.copy()方法
- 使用TTS产生的错误
- setter方法中copy使用错误
- Java 使用内部类时候的容易产生的错误
- TreeView 回调时产生错误的处理方法
- java.util.Collections.copy()方法注意点
- java.util.Collections.copy()方法注意点
- java.util.Collections.copy()方法注意点
- java.util.Collections.copy()方法注意点
- java.util.Collections.copy()方法注意点
- java.util.Collections.copy()方法注意点
- java.util.Collections.copy()方法注意点
- java 产生OutOfMemoryError错误
- Android数据存储方式:SharePreference、SQLite、ContentProvider有什么不同?
- HDU2539(模拟)
- 周鸿祎:致那些在公司混日子的人
- 创建线程及其实现
- 【剑指Offer面试编程题】题目1512:用两个栈实现队列--九度OJ
- [Java错误]使用Collections中的copy方法复制ArrayList产生的错误
- 选择排序算法
- 黑马程序员——Java语言基础——05.String类及其相关类
- Palindrome Partitioning II
- WordPress主题制作全过程(一):基础准备
- update-alternatives学习笔记
- 同乐公司的接口性能指标及标签的作用
- WordPress主题制作全过程(二):主题文件构成
- ruby on rails 环境搭建以及emacs开发环境配置