package com.dzh.Collections;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author mocking
* warning 慎用List的subList(fromIndex,toIndex)方法,subList的值发生改变,list的值会发生相应的改变
* list 的结构发生变化时,subList方法会抛出 java.util.ConcurrentModificationException 异常
* subList的结构发生变化时,list也会发生相应的变化
*/
public class SubListTest {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for(int i = 0;i< 10;i++)
list.add(i);
List<Integer> subList = list.subList(0, 4);
/*List<Integer> newList = subList;*/
List<Integer> newList = new ArrayList<>();
for(int i = 0;i < subList.size();i++)
newList.add(subList.get(i));
System.out.println("subList:"+subList);
System.out.println("list:"+list);
System.out.println("newList:"+newList);
subList.remove(3);
System.out.println("subList:"+subList);
System.out.println("list:"+list);
System.out.println("newList:"+newList);
/* list.remove(5);
System.out.println("subList:"+subList);
System.out.println("list:"+list);
System.out.println("newList:"+newList);*/
/* subList.clear();
for(int i = 1;i < 5;i++)
subList.add(i);
System.out.println("sublist---"+subList);
System.out.println("list---"+list);
System.out.println("newList---"+newList);*/
}
}
import java.util.ArrayList;
import java.util.List;
/**
*
* @author mocking
* warning 慎用List的subList(fromIndex,toIndex)方法,subList的值发生改变,list的值会发生相应的改变
* list 的结构发生变化时,subList方法会抛出 java.util.ConcurrentModificationException 异常
* subList的结构发生变化时,list也会发生相应的变化
*/
public class SubListTest {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for(int i = 0;i< 10;i++)
list.add(i);
List<Integer> subList = list.subList(0, 4);
/*List<Integer> newList = subList;*/
List<Integer> newList = new ArrayList<>();
for(int i = 0;i < subList.size();i++)
newList.add(subList.get(i));
System.out.println("subList:"+subList);
System.out.println("list:"+list);
System.out.println("newList:"+newList);
subList.remove(3);
System.out.println("subList:"+subList);
System.out.println("list:"+list);
System.out.println("newList:"+newList);
/* list.remove(5);
System.out.println("subList:"+subList);
System.out.println("list:"+list);
System.out.println("newList:"+newList);*/
/* subList.clear();
for(int i = 1;i < 5;i++)
subList.add(i);
System.out.println("sublist---"+subList);
System.out.println("list---"+list);
System.out.println("newList---"+newList);*/
}
}