Java的可选操作的方法

注《Java编程思想》第四版第507页
可选操作应该是这样的:
像Collection接口,提供了add方法,他是可选的;这时你想要实现一个自定义的MyList,它是只读的,这是你可以不实现add方法,如果调用了add方法,则会报一个 UnsupportedOperationExpection错误(未获得支持异常)。
Collection接口的添加和移除的方法都是可选方法。

例如:
  • add
    boolean add(E e)
    Ensures that this collection contains the specified element  (optional operation)<可选操作>. Returns   true  if this collection changed as a result of the call. (Returns   false  if this collection does not permit<允许> duplicates<重复> and already contains the specified element.)

    Collections that support this operation may place limitations on what elements may be added to this collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.

    If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.

    Parameters:
    e  - element whose presence in this collection is to be ensured
    Returns:
    true  if this collection changed as a result of the call
    Throws:
    UnsupportedOperationException  - if the   add  operation is not supported by this collection
    ClassCastException  - if the class of the specified element prevents it from being added to this collection
    NullPointerException  - if the specified element is null and this collection does not permit null elements
    IllegalArgumentException  - if some property of the element prevents it from being added to this collection
    IllegalStateException  - if the element cannot be added at this time due to insertion restrictions


 JDK里面Collection接口的add方法和remove方法的实现如下(Java安装目录下的src文件):
 public boolean add(E e) {
            throw new UnsupportedOperationException();
        }
        public boolean remove(Object o) {
            throw new UnsupportedOperationException();
        }
        public boolean addAll(Collection<? extends E> coll) {
            throw new UnsupportedOperationException();
        }
        public boolean removeAll(Collection<?> coll) {
            throw new UnsupportedOperationException();
        }
        public boolean retainAll(Collection<?> coll) {
            throw new UnsupportedOperationException();
        }
        public void clear() {
            throw new UnsupportedOperationException();
        }

这样的方法你必须自己去实现它的操作,否则你调用的回事Collections的方法,会抛出未获得支持异常。
如果对一个 ArrayList 调用 ArrayList.asList(),它会变成一个数组结构的List,它不支持add或remove等改变容器尺寸的方法,他就是直接使用了Collections的接口,所以调用add或remove会抛出 UnsupportedOperationException。             

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值