`
MouseLearnJava
  • 浏览: 468361 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

认识Arrays.asList方法

    博客分类:
  • Java
阅读更多
Arrays工具类提供了一些比较实用的方法,比如sort, binarySearch, fill等。其中还有一个asList方法,此方法能够将一个变长参数或者数组转换成List。
但是,这个生成的List,它是固定长度的,如果对其进行add或者remove的操作,会抛出UnsupportedOperationException,为什么会这样呢?

带着疑问,查看一下Arrays的源码,可以得到问题的结果。


  
 /**
     * 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 <tt>Collection.toArray</tt>.  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>
     *     List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
     * </pre>
     *
     * @param a the array by which the list will be backed.
     * @return a list view of the specified array.
     * @see Collection#toArray()
     */
    public static <T> List<T> asList(T... a) {
	return new ArrayList<T>(a);
}


方法asList返回的是new ArrayList<T>(a)。但是,这个ArrayList并不是java.util.ArrayList,它是一个Arrays类中的重新定义的内部类。

具体的实现如下:

/**
     * @serial include
     */
    private static class ArrayList<E> extends AbstractList<E>
	implements RandomAccess, java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
	private Object[] a;

	ArrayList(E[] array) {
            if (array==null)
                throw new NullPointerException();
	    a = array;
	}

	public int size() {
	    return a.length;
	}

	public Object[] toArray() {
	    return (Object[])a.clone();
	}

	public E get(int index) {
	    return (E)a[index];
	}

	public E set(int index, E element) {
	    Object oldValue = a[index];
	    a[index] = element;
	    return (E)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;
        }
    }


从这个内部类ArrayList的实现可以看出,它继承了类AbstractList<E>,但是没有重写add和remove方法,没有给出具体的实现。查看一下AbstractList类中对add和remove方法的定义,如果一个list不支持add和remove就会抛出UnsupportedOperationException。

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
    /**
     * Sole constructor.  (For invocation by subclass constructors, typically
     * implicit.)
     */
    protected AbstractList() {
}

/**
     * Appends the specified element to the end of this List (optional
     * operation). <p>
     *
     * This implementation calls <tt>add(size(), o)</tt>.<p>
     *
     * Note that this implementation throws an
     * <tt>UnsupportedOperationException</tt> unless <tt>add(int, Object)</tt>
     * is overridden.
     *
     * @param o element to be appended to this list.
     * 
     * @return <tt>true</tt> (as per the general contract of
     * <tt>Collection.add</tt>).
     * 
     * @throws UnsupportedOperationException if the <tt>add</tt> method is not
     * 		  supported by this Set.
     * 
     * @throws ClassCastException if the class of the specified element
     * 		  prevents it from being added to this set.
     * 
     * @throws IllegalArgumentException some aspect of this element prevents
     *            it from being added to this collection.
     */
    public boolean add(E o) {
	add(size(), o);
	return true;
    }

    /**
     * Inserts the specified element at the specified position in this list
     * (optional operation).  Shifts the element currently at that position
     * (if any) and any subsequent elements to the right (adds one to their
     * indices).<p>
     *
     * This implementation always throws an UnsupportedOperationException.
     *
     * @param index index at which the specified element is to be inserted.
     * @param element element to be inserted.
     * 
     * @throws UnsupportedOperationException if the <tt>add</tt> method is not
     *		  supported by this list.
     * @throws ClassCastException if the class of the specified element
     * 		  prevents it from being added to this list.
     * @throws IllegalArgumentException if some aspect of the specified
     *		  element prevents it from being added to this list.
     * @throws IndexOutOfBoundsException index is out of range (<tt>index <
     *		  0 || index > size()</tt>).
     */
    public void add(int index, E element) {
	throw new UnsupportedOperationException();
    }

    /**
     * Removes the element at the specified position in this list (optional
     * operation).  Shifts any subsequent elements to the left (subtracts one
     * from their indices).  Returns the element that was removed from the
     * list.<p>
     *
     * This implementation always throws an
     * <tt>UnsupportedOperationException</tt>.
     *
     * @param index the index of the element to remove.
     * @return the element previously at the specified position.
     * 
     * @throws UnsupportedOperationException if the <tt>remove</tt> method is
     *		  not supported by this list.
     * @throws IndexOutOfBoundsException if the specified index is out of
     * 		  range (<tt>index < 0 || index >= size()</tt>).
     */
    public E remove(int index) {
	throw new UnsupportedOperationException();
    }

}


至此,为什么Arrays.asList产生的List是不可添加或者删除,否则会产生UnsupportedOperationException,就可以得到解释了。

如果我们想把一个变长或者数据转变成List, 而且期望这个List能够进行add或者remove操作,那该怎么做呢?

我们可以写一个类似的方法,里面直接采用java.util.ArrayList即可。

比如:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyArrays {

	public static <T> List<T> asList(T... a) {
		List<T> list = new ArrayList<T>();
		Collections.addAll(list, a);
		return list;
	}
}


测试代码如下:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {

		List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
		print(stooges);

		List<List<String>> seasonsList = Arrays.asList(retrieveSeasonsList());
		print(seasonsList);

		/*
		 * 自己实现一个asList方法,能够添加和删除。
		 */
		List<String> list = MyArrays.asList("Larry", "Moe", "Curly");
		list.add("Hello");
		print(list);

	}

	private static <T> void print(List<T> list) {
		System.out.println(list);
	}

	private static List<String> retrieveSeasonsList() {
		List<String> seasonsList = new ArrayList<String>();
		seasonsList.add("Spring");
		seasonsList.add("Summer");
		seasonsList.add("Autumn");
		seasonsList.add("Winter");
		return seasonsList;
	}

}


输出结果:

[Larry, Moe, Curly]
[[Spring, Summer, Autumn, Winter]]
[Larry, Moe, Curly, Hello]

3
2
分享到:
评论

相关推荐

    能让你成为一个优秀的 Java 全栈程序员的系统化系列教程

    Arrays.asList 解析 Comparable 和 Comparator的理解 并发系列: JSR-133 都解决了哪些问题 简单认识并发 看完你就明白的锁系列之锁的状态 看完你就明白的锁系列之乐观锁和悲观锁 看完你就明白的锁系列之自旋锁 锁...

    两两认识leetcode-Code_Record:这个仓库是我的解决方案和Leetcode问题的一些笔记

    Arrays.asList 不处理装箱,只会创建一个 List 这不是你想要的。 你必须制定一个实用方法。 映射到字符串 Arrays.toString(map.entrySet().toArray()) 数组到字符串 一System.out.println(Arrays.toString(array))...

    Java 实例 - Varargs 可变参数使用源代码-详细教程.zip

    例如,`Arrays.asList(1, 2, 3)`返回一个List对象,而无需创建数组。 5. 示例代码: 让我们看一个具体的实例,这个方法用于计算并打印给定数字的最大值: ```java public static void printMax(int... nums) { ...

    算法设计-格雷码的分治构造算法

    return Arrays.asList(0, 1); } else { List&lt;Integer&gt; left = grayCode(n - 1); List&lt;Integer&gt; right = new ArrayList(); for (int num : left) { right.add((1 (n - 1)) + num); } Collections.reverse...

    认识类集、Collection接口

    list.addAll(Arrays.asList("D", "E")); for (String item : list) { System.out.println(item); } // 删除元素 list.remove("C"); list.removeIf(s -&gt; s.startsWith("A")); // 检查元素 System.out....

    1z0-809 128q.pdf(ocp jdk8)-CSDN下载

    在给定的代码片段中,使用了`Arrays.asList`将数组转换为列表,并利用流操作进行了处理。另外,还有使用`HashMap`和`TreeMap`对键值对进行操作,这考察了考生对不同集合类特性和使用场景的掌握。 知识点六:接口与...

    strust2 checkboxlist

    private List&lt;String&gt; options = Arrays.asList("Option1", "Option2", "Option3"); // getter and setter for options ``` 5. **结果处理与模型驱动** 当用户提交表单后,Struts2会自动将选中的值以数组形式...

Global site tag (gtag.js) - Google Analytics