A common method of understanding java-15.2 collection from the Beginning (2)-Attention point

Source: Internet
Author: User

In the previous section we talked about the common methods of collection, as well as the previous chapters where we introduced the AddAll method of collection, which we introduced to its point of attention.

Note that, in the common method, the basic is optional operation.

What is an optional operation?

Unfortunately, the method of operation is not set for all implementations, but only for a certain class of implementation.

For example:

Package Com.ray.ch15;import Java.util.arrays;import Java.util.collection;import Java.util.list;public class Test { public static void Test (list<string> List, String msg) {System.out.println ("---------" + msg + "--------"); list<string> sublist = list.sublist (1, 4); Collection<string> Collection = list;try {collection.retainall (sublist);} catch (Exception e) { System.out.println ("Retainall:" + E);}} public static void Main (string[] args) {list<string> list = Arrays.aslist ("1", "2", "3", "4", "5", "6"), Test (list, " Start1 ");}}

Output:

---------Start1--------
RetainAll:java.lang.UnsupportedOperationException


Why does it throw an exception?

Because Arrays.aslist is the lowest-level data structure of this method is an array, the array in Java is not modifiable, when retainall this method to modify the data structure inside the array, it is not allowed.

If we output a bit more exception information above, we will locate the Abstractlist remove method, and when it is called, it will throw an unsupported exception.

Public E Remove (int index) {throw new unsupportedoperationexception ();    }


Summary: This chapter focuses on the optional operation of the common methods of collection.


This chapter is here, thank you.

-----------------------------------

Directory




A common method of understanding java-15.2 collection from the Beginning (2)-Attention point

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.