1. ArrayList's sublist
The result can not be strongly turned into ArrayList, otherwise throw classcastexception exception, because Sublist returned is ArrayList's inner class sublist, not ArrayList, but a view of ArrayList. Example
Public class sublisttest { publicstaticvoid main (string[] args) { ArrayList New arraylist<integer>(); Mylist.add (1); Mylist.add (2); Mylist.add (3); Mylist.add (4); List<Integer> sublist = mylist.sublist (1, 2); System.out.println (myList); System.out.println (sublist); }}
Output
[1, 2, 3, 4] [2]
The
operation for Sublist will eventually be reflected in the original list, such as
Public class sublisttest { publicstaticvoid main (string[] args) { ArrayList New arraylist<integer>(); Mylist.add (1); Mylist.add (2); Mylist.add (3); Mylist.add (4); List<Integer> sublist = mylist.sublist (1, 2); Sublist.add (5); System.out.println (myList); System.out.println (sublist); }}
Results
[1, 2, 5, 3, 4] [2, 5]
Note that the modification of the original list causes the concurrentmodificationexception exception to be traversed, added, and deleted by the child list, for example
Public class sublisttest { publicstaticvoid main (string[] args) { ArrayList New arraylist<integer>(); Mylist.add (1); Mylist.add (2); Mylist.add (3); Mylist.add (4); List<Integer> sublist = mylist.sublist (1, 2); Mylist.add (5); System.out.println (myList); System.out.println (sublist); }}
Results
Exception in thread "main" java.util.ConcurrentModificationException at java.util.arraylist$ Sublist.checkforcomodification (arraylist.java:1237) at Java.util.arraylist$sublist.listiterator ( arraylist.java:1097) at java.util.AbstractList.listIterator (abstractlist.java:299) at Java.util.arraylist$sublist.iterator (arraylist.java:1093) at java.util.AbstractCollection.toString ( abstractcollection.java:454) at java.lang.String.valueOf (string.java:2994) at Java.io.PrintStream.println (printstream.java:821) at Sublisttest.main (sublisttest.java:16) [1, 2, 3, 4, 5]
2. Set-to-go array toarray
The recommended size is list.size (). If it is less than required, ToArray will reallocate the memory space and return the array address: If the array element is greater than required, the excess will be null. Example
Public class toarraytest { publicstaticvoid main (string[] args) { List New arraylist<integer>(); Mylist.add (1); Mylist.add (2); Mylist.add (3); New integer[mylist.size ()]; Mylist.toarray (MyArray); System.out.println (myList); System.out.println (myarray.length); }}
Results
[1, 2, 3]3
Note: If you use List.toarray () without parameters directly, the object[] class is returned, the classcastexception exception will occur if you force the conversion to an array
3. Using Aslist to convert an array into a set
Do not increase or delete the resulting results, or you will throw unsupportedoperationexception exceptions, for example
Public class aslisttest { publicstaticvoid main (string[] args) { new integer[] {1, 2, 3}; List<Integer> myList = arrays.aslist (myArray); Mylist.add (2); // Mylist.remove (4); }}
Results
Exception in thread "main" java.lang.UnsupportedOperationException at Java.util.AbstractList.add ( abstractlist.java:148) at Java.util.AbstractList.add (abstractlist.java:108) at Aslisttest.main ( Aslisttest.java:9)
Reason: Aslist returned is the inner class of arrays, and does not implement the collection of modification methods, just the embodiment of the adapter mode, only the conversion interface, the background is actually an array, so that the data to delete and deletion is certainly not possible. Since the background is an array, the modification of the element is still possible, such as
public class Aslisttest { static void main (string[] args) {intege r[] MyArray = new integer[] {1, 2, 3}; List <Integer> myList = Arrays.aslist (MyArray); // Mylist.add (2); // Mylist.remove (4); MYARRAY[1] = 5; System.out.println (myarray[ 1 1
Results
55
Java sublist, ToArray, aslist note points