Arrays.asList() :
int [] ints = {1,2,3,4}List list = Arrays.asList(ints);
list.size() ->>>> 1 not 4
because :
public static <T> asList(T...a){
return new ArrayList<T>(a); }
the input is generic type values T, but as ints is an int[], int is basic type , cannot be generilized.
so we can :
Integer [] ints = {1,2,3,4}
List list = Arrays.asList(ints);
list.size() ->>>> 4
but cannot list.add(1) // ->>> the ArrayList<T> here is not the java.util.ArrayList, it's an inner class of java.util.Arrays
List.subList() :
List<int> list1 = new ArrayList<int>(); list1.add(1); list1.add(2);List<int> list2 = new ArrayList<int>(list1);
List<int> list3 = list1.subList(0, list.size());
list3.add(3);
...
list1.equals(list2) >>>>false // not the same address
list1.equals(list3) >>>>true // same address ---> subList return a SubList, it's a inner class of ArrayList, all the operations are applied on the original list. i's just a mirror of original list.
after using subList(), we should avoid doing operations on the original list : the modCount is copied to subCList too, so when we do the operation on the original list, there will be fail fast.
So, what sublist is for ?
it's for doing operation on some indexes of the list :
list1.subList(100,200).clear(); = for(int i = 0;...){ if(i>=100&&i<=200) {list1.remove(i);}}
No comments:
Post a Comment