Variable Parameter Method for new features of JDK 5 (2) --- asList, JDK 5 --- aslist

Source: Internet
Author: User

Variable Parameter Method for new features of JDK 5 (2) --- asList, JDK 5 --- aslist

"Arrays. asList (T... A) use of methods

"UnsupportedOperationException Analysis

 

 

  • Arrays. asList (T... A) use of methods
        
Package cn. itcast. day24.varparam; import java. util. arrays; import java. util. list;/***** public static <T> List <T> asList (T... a): convert an array to a set ** Note: * The essence of the converted set is an array, and the length is fixed. Therefore, the converted set cannot be added or deleted (the length of the array will be changed ), it can only be modified. **/Public class AsListDemo {public static void main (String [] args) {// define an array // String [] arrStr = {"Lin Qingxia", "Wu Song ", ""}; // List <String> list = Arrays. asList (arrStr); List <String> list = Arrays. asList ("java", "world", "hello"); // UnsupportedOperationException list. add ("javaee"); // error // list. remove (1); // Error list. set (1, "javaee"); // modified successfully // for (String s: list) {// System. out. println (s );//}}}
  • UnsupportedOperationException Analysis
  

We can see that Arrays's private internal class Arrays $ ArrayList does not implement length-related methods such as add () and remove () from the parent class javasactlist,

Therefore, an UnsupportedOperationException is thrown, which means:Request operations (of course not supported)

      
Private static class ArrayList <E> extendsAbstractList<E> implements RandomAccess, java. io. serializable {private static final long serialVersionUID =-2764017481108945198L; private final E [] a; ArrayList (E [] array) {if (array = null) throw new NullPointerException (); a = array;} public int size () {return. length;} public Object [] toArray () {return. clone () ;}public <T> T [] toArray (T [] a) {int size = size (); if (. length <size) return Arrays. CopyOf (this. a, size, (Class <? Extends T []>). getClass (); System. arraycopy (this. a, 0, a, 0, size); if (. length> size) a [size] = null; return a;} public E get (int index) {return a [index];}// Rewrite the set Method of the parent class AbstractList, so no exception is thrown during set, and the add and remove methods are from the base class, so an exception is thrown.
    public E set(int index, E element) {        E oldValue = a[index];        a[index] = element;        return oldValue;    }        public int indexOf(Object o) {            if (o==null) {                for (int i=0; i<a.length; i++)                    if (a[i]==null)                        return i;            } else {                for (int i=0; i<a.length; i++)                    if (o.equals(a[i]))                        return i;            }            return -1;        }        public boolean contains(Object o) {            return indexOf(o) != -1;        }    }
  

 

  
AbstractList source code:
      
package java.util;public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {    public E set(int index, E element) {    throw new UnsupportedOperationException();    }    public boolean add(E e) {    add(size(), e);    return true;    }    public void add(int index, E element) {    throw new UnsupportedOperationException();    }    public E remove(int index) {    throw new UnsupportedOperationException();    }    public void clear() {        removeRange(0, size());    }    public E set(int index, E element) {    throw new UnsupportedOperationException();    }    abstract public E get(int index);}

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.