AddAll Anomaly of a set crawl pit
Background: There are often such requirements, we need to copy the value of the set A to set B, or to the collection of multiple arrays to the statistical value, then it is not feasible, just yesterday a friend encountered a mistake, we discussed together.
Normally, we implement adding the value of collection A to the set B, which is the case:
list<string> list3 = new arraylist<> ();
List3.add ("1");
list<string> list4 = new arraylist<> ();
List4.add ("2");
List3.addall (LIST4);
There's nothing wrong with that, old iron, but that friend said he called the AddAll method and reported the following error:
Exception in thread "main" java.lang.UnsupportedOperationException
At Java.util.AbstractList.add (abstractlist.java:148)
At Java.util.AbstractList.add (abstractlist.java:108)
At Java.util.AbstractCollection.addAll (abstractcollection.java:344)
And then he said that when debugging, the value of the two set is normal, which is odd, so the trace source discovers that his collection is transformed by an array, for example:
List<string> List1 = arrays.aslist ("1", "2", "3");
List<string> List2 = Arrays.aslist ("4", "5", "6");
List1.addall (LIST2);
Usually a lot of people like to convert arrays into collections for ease of operation, and the JDK has exactly the same approach, arrays.aslist, let's look at his internal implementations
/**
* Returns a fixed-size list backed by the specified array. (Changes to
* The returned list "write through" to the array.) This method acts
* As bridge between array-based and Collection-based APIs, in
* Combination with {@link Collection#toarray}. The returned list is
* Serializable and implements {@link randomaccess}.
*
* <p>this method also provides a convenient to create a fixed-size
* List initialized to contain several elements:
* <pre>
* list< String> Stooges = Arrays.aslist ("Larry", "Moe", "Curly");
* </pre>
*
* @param <T> The class of the objects in the array
* @param a The array by which the list would be backed
* @return A list view of the specified array
*/
@SafeVarargs
@SuppressWarnings ("VarArgs")
public static <T> list<t> aslist (T ... a) {
return new Arraylist<> (a);
}
We can tell by the method above that the array is converted to a collection by such a call, and the size of the returned collection is fixed, so adding the element inside is definitely an error. So what is the correct posture to solve this problem, slightly modified to:
Wrong posture
List<string> List1 = arrays.aslist ("1", "2", "3");
List<string> List2 = Arrays.aslist ("4", "5", "6");
List1.addall (LIST2);
Correct posture
list<string> list3 = new Arraylist<> (Arrays.aslist ("1", "2", "3"));
list<string> list4 = new Arraylist<> (Arrays.aslist ("1", "2", "3"));
List3.addall (LIST4);
By the way, look at some of the differences between arrays and collections:
An array declares the type of the element it holds, and the collection does not declare it. This is because the collection stores their elements in object form.
An array instance has a fixed size and cannot be scaled. The collection can dynamically change the size as needed.