Home >Java >javaTutorial >Java source code analysis Arrays.asList method detailed explanation

Java source code analysis Arrays.asList method detailed explanation

高洛峰
高洛峰Original
2017-02-04 10:11:521502browse

Recently, I took the time to analyze the source code of the asList method of the java Arrays tool class, compiled relevant information on the Internet, and recorded it. I hope it can also help readers!

The Arrays tool class provides a method asList, which can be used to convert a variable-length parameter or array into a List.

The source code is as follows:

/**
 * 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 way to create a fixed-size
 * list initialized to contain several elements:
 * <pre class="brush:php;toolbar:false">
 *  List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
 * 
* * @param a the array by which the list will be backed * @return a list view of the specified array */ @SafeVarargs public static List asList(T... a) { return new ArrayList<>(a); }

Problem discovery

According to the description of the above method, let’s write a few examples first:

/**
 * @author wangmengjun
 *
 */
public class ArrayExample {
  
 public static void main(String[] args) {
   
  /**使用变长参数*/
  List<String> array1 = Arrays.asList("Welcome", "to","Java", "world");
  System.out.println(array1);
   
  /**使用数组*/
  List<String> array2 = Arrays.asList(new String[] {"Welcome", "to","Java", "world"});
  System.out.println(array2);
 }
 
}

Run the above program , output the following content.

[Welcome, to, Java, world]
[Welcome, to, Java, world]

On a whim, I suddenly want to add a string to the created list" Cool~~~", Go one.

/**使用变长参数*/
 List<String> array1 = Arrays.asList("Welcome", "to","Java", "world");
 array1.add("Cool~~~");

As a result, an UnsupportedOperationException was encountered:

Exception in thread "main" java.lang.UnsupportedOperationException
 at java.util.AbstractList.add(Unknown Source)
 at java.util.AbstractList.add(Unknown Source)
 at test.ArrayExample.main(ArrayExample.java:36)

Incredibly, the list generated by new ArrayLista8093152e673feb7aba1828c43532094(a) encountered a problem when calling the add method.

Cause search

Then the question is, what happened? With questions in mind, let’s check out what the ArrayList used in Arrays.asList looks like?

It turns out that the ArrayList class used by the asList method of Arrays is an internally defined class, not the java.util.ArrayList class.

The source code is as follows:

/**
  * @serial include
  */
 private static class ArrayList<E> extends AbstractList<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 a.length;
   }
 
   public Object[] toArray() {
     return a.clone();
   }
 
   public <T> T[] toArray(T[] a) {
     int size = size();
     if (a.length < size)
       return Arrays.copyOf(this.a, size,
                  (Class<? extends T[]>) a.getClass());
     System.arraycopy(this.a, 0, a, 0, size);
     if (a.length > size)
       a[size] = null;
     return a;
   }
 
   public E get(int index) {
     return a[index];
   }
 
   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;
   }
 }

It can be seen from the implementation of this internal class ArrayList that it inherits the abstract class java.util. AbstractList1a4db2c2c2313771e5742b6debf617a1, but the add and remove methods are not rewritten and no specific implementation is given.

However, by default, the java.util.AbstractList class will directly throw UnsupportedOperationException in the add, set and remove methods. Part of the source code of AbstractList is as follows:

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
  /**
   * Sole constructor. (For invocation by subclass constructors, typically
   * implicit.)
   */
  protected AbstractList() {
  }
 
  public E set(int index, E element) {
    throw new UnsupportedOperationException();
  }
 
  /**
   * {@inheritDoc}
   *
   * <p>This implementation always throws an
   * {@code UnsupportedOperationException}.
   *
   * @throws UnsupportedOperationException {@inheritDoc}
   * @throws ClassCastException      {@inheritDoc}
   * @throws NullPointerException     {@inheritDoc}
   * @throws IllegalArgumentException   {@inheritDoc}
   * @throws IndexOutOfBoundsException   {@inheritDoc}
   */
  public void add(int index, E element) {
    throw new UnsupportedOperationException();
  }
 
  /**
   * {@inheritDoc}
   *
   * <p>This implementation always throws an
   * {@code UnsupportedOperationException}.
   *
   * @throws UnsupportedOperationException {@inheritDoc}
   * @throws IndexOutOfBoundsException   {@inheritDoc}
   */
  public E remove(int index) {
    throw new UnsupportedOperationException();
  }
}

It is precisely because the internal class ArrayList of the java.util.Arrays class does not override the add and remove methods, so when When we call its add method, we actually call the add method of the AbstractList class, and the result is that an UnsupportedOperationException is thrown directly.

Similarly, you will also encounter UnsupportedOperationException when calling the remove method, or calling other methods associated with the add and remove methods (such as addAll).

Example of addAll:

/**
 * @author wangmengjun
 *
 */
public class ArrayExample {
 
  public static void main(String[] args) {
 
    /**使用变长参数*/
    List<String> array1 = Arrays.asList("Welcome", "to", "Java", "world");
    array1.addAll(Arrays.asList("AAA", "BBB"));
  }
 
}

Exception in thread "main" java.lang.UnsupportedOperationException
 at java.util.AbstractList.add(Unknown Source)
 at java.util.AbstractList.add(Unknown Source)
 at java.util.AbstractCollection.addAll(Unknown Source)
 at test.ArrayExample.main(ArrayExample.java:36)

Example of

set:

/**
 * @author wangmengjun
 *
 */
public class ArrayExample {
 
  public static void main(String[] args) {
 
    /**使用变长参数*/
    List<String> array1 = Arrays.asList("Welcome", "to", "Java", "world");
    System.out.println(array1);
     
    //将Java替换成hello
    array1.set(2, "hello");
    System.out.println(array1);
  }
 
}

It is precisely because the internal class ArrayList of Arrays overrides the set method that the above program can run normally and will no longer throw UnsupportedOperationException.

The results are as follows:

[Welcome, to, Java, world]
[Welcome, to, hello, world]

Usage scenarios

Judging from the above examples and simple analysis, the Arrays tool class provides a method asList, which can be used to convert a variable-length parameter or array into a List.

However, the length of the generated List is fixed; modification operations can be performed (for example, modifying an element at a certain position); operations that affect the length (such as add, remove, etc.) cannot be performed. An UnsupportedOperationException will be thrown.

Arrays.asList is more suitable for scenarios where you already have array data or some elements and need to quickly build a List, which is only used for reading operations without adding or deleting operations.

If you want to quickly obtain a list that can be added, deleted, modified, and checked based on known array data, a relatively simple method is as follows:

Reuse the java.util.ArrayList wrapper layer.

/**
 * @author wangmengjun
 *
 */
public class ArrayExample {
 
  public static void main(String[] args) {
 
    /**使用变长参数*/
    List<String> array1 = new ArrayList<>(Arrays.asList("Welcome", "to", "Java", "world"));
    System.out.println(array1);
 
    array1.add("Cool~~~");
    System.out.println(array1);
 
  }
 
}

The results are as follows:

[Welcome, to, Java, world]
[Welcome, to, Java, world, Cool ~~~]

Thank you for reading, I hope it can help everyone, thank you for your support of this site!

For more java source code analysis and detailed explanation of the Arrays.asList method, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn