![]() List bookList = new ArrayList() bookList.add("The Distant Savior") bookList.add("Betrayal") bookList.add("Sky Red Dust") bookList.add( "Life") bookList.add("Ordinary World") List luyaoBookList = bookList.subList(3, 5) (bookList) (luyaoBookList) / / Modify the value of the original collection t(3,"Luyao-Life") (bookList) (luyaoBookList) The above points are described in the "Alibaba Java Development Manual" Taishan Edition:Ģ.1 Modifying the value of the original collection will affect the sub-collectionįor example, we modify the value of an element in the original collection bookList (non-structural modification): Modifying the value of the original collection element will affect the sub-collectionModifying the structure of the original collection will cause ConcurrentModificationExceptionModifying the value of a sub-collection element will affect the original collectionModifying the structure of a subcollection will affect the original collection It is very simple to use and easy to understand, but there are still the following points to pay attention to, otherwise it will cause program errors or exceptions: The running result is shown in the figure below:Īs can be seen from the running results, what subList returns is the set of elements whose index from fromIndex (included) to toIndex (excluded) in bookList is returned. Let’s first look at the simple use of subList: List bookList = new ArrayList() bookList.add("The Distant Savior") bookList.add("Betrayal") bookList.add("Sky Red Dust") bookList.add( "Life") bookList.add("Ordinary World") List luyaoBookList = bookList.subList(3, 5) (bookList) (luyaoBookList) The Arrays.asList method can be used in some simple situations, such as quickly declaring a collection to determine whether a value is within the allowed range:īut do not call methods such as add to modify the collection after declaration, otherwise will be reported. So everyone should pay attention when using Arrays.asList to avoid stepping on the pit. When using the tool class Arrays.asList() to convert an array into a collection, you cannot use its methods related to modifying the collection, and its add/remove/clear methods will throw an UnsupportedOperationException exception. Regarding this point, it is also mentioned in the “Alibaba Java Development Manual” Taishan Edition: ![]() ![]() It also inherits the AbstractList class and rewrites many methods, such as the contains method we used above, but it does not rewrite the add method, so we will throw when we call the add method. The returned ArrayList is very familiar, there is nothing there, but if you take a closer look, you will find that this ArrayList is not the ArrayList we often use, because the ArrayList we usually use is located under the java.util package:īut the ArrayList here is an inner class of the Arrays class: With this question in mind, let’s look at the source code of the static method asList provided by the Arrays class: ![]() I can’t help but wonder why such an exception is thrown simply by adding an element. The expected result should be to output true, but actually a is thrown: Then, add element 3 to statusList, as follows:
0 Comments
Leave a Reply. |