Arrays.asList()使用注意点
今天看代码时, 发现书上使用了Arrays.asList()方法, 将一个数组转成了List, 然后说到得到的List不能调用add(), remove()方法添加元素或者删除,带着疑问看了下内部实现原理, 看了别人博客明白了, 为啥不能调用add(), remove()方法了.下面是转载的一篇文章.
转载:http://www.lai18.com/content/10306565.html
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来接受Arrays.asList(array)返回的参数、原因是Arrays.asList(array)返回的虽然不是java.util.ArrayList但是返回的ArrayList同理也继承自AbstractList
我们的AbstractList实现自List接口、所以可以用list接口来引用Arrays.asList(array);
如果我们想把一个变长或者数据转变成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]
Arrays.asList()使用注意点的更多相关文章
- 【转】java.util.Arrays.asList 的用法
DK 1.4对java.util.Arrays.asList的定义,函数参数是Object[].所以,在1.4中asList()并不支持基本类型的数组作参数. JDK 1.5中,java.util.A ...
- Arrays.asList()注意
api: public static <T> List<T> asList(T... a) 返回一个受指定数组支持的固定大小的列表.(对返回列表的更改会“直接写”到数组.)此方 ...
- Arrays.toString Arrays.asList
import java.util.Arrays; public class TestCalc{ public static void main(String[] args) { ,,,,,,,}; / ...
- Arrays.asList(数组) 解说
最近在用Arrays的asList()生成的List时,List元素的个数时而不正确. Java代码 一:Arrays.asList(数组)该方法是将数组转化为集合(该方法主要用于Object对象数组 ...
- Arrays.asList方法总结
import java.util.Arrays; import java.util.List; /** * * 本类演示了Arrays类中的asList方法 * 通过四个段落来演示,体现出了该方法的相 ...
- Arrays.asList的使用及异常问题
将数组转成List问题,通常我们习惯这样写成:List<String> list = Arrays.asList("1","2"); 于是我们这样就 ...
- Arrays.asList引起的惨案
最近代码中需要对两个数组求交,想当然便用到了List中的retainAll函数,但要将将数组转换成list.代码如下: String[] abc = new String[] { "abc& ...
- Arrays.asList的源码分析
以前一直很奇怪为什么Arrays.asList的数组不能插入新的数据,后来看了源码发现是因为内部是一个final的数组支持起来的Arraylist,下面贴入源码与分析. 1.先看Arrays的方法 我 ...
- 【转载】最近在用Arrays的asList()生成的List时,List元素的个数时而不正确,数组转化为List,即Arrays.asList(intArray);
最近在用Arrays的asList()生成的List时,List元素的个数时而不正确. Java代码 //经多次测试,只要传递的基本类型的数组,生成List的元素个数均为1 char arrc = { ...
随机推荐
- HTML部分标签和代码
1.1.1 一般标签.格式控制标签 所谓HTML就是 Hyper Text Markup Language(超无文本标记语言) <html> 开始标签 <head> 网页上 ...
- 强连通(hdu4635)最多增加几条单向边后满足最终的图形不是强连通
Strongly connected Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- poj 题目分类(2)
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj329 ...
- 去掉字符串中的空格 JS JQ 正则三种不同写法
<script> function trim(str) { return str.replace(/(^\s*|\s*$)/g, "") } console.log(t ...
- Aptana Studio3开发Python和Ruby(最佳工具)
即从: http://d1iwq2e2xrohf.cloudfront.net/tools/studio/standalone/3.3.1.201212171919/win/Aptana_Studio ...
- php防止刷新(流量攻击)的一段代码
<?php//查询禁止IP$ip =$_SERVER['REMOTE_ADDR'];$fileht=".htaccess2";if(!file_exists($fileht) ...
- MapReduce之Writable相关类
当要在进程间传递对象或持久化对象的时候,就需要序列化对象成字节流,反之当要将接收到或从磁盘读取的字节流转换为对象,就要进行反序列化.Writable是Hadoop的序列化格式,Hadoop定义了这样一 ...
- google pinyin elmentary os
sudo apt-get install software-properties-common for ppa. I have been using Sun Pinyin for quite a lo ...
- [转载] 关关采集不能生成html的问题
转载地址:http://www.pcliver.com/archives/814.html 关关不能生成html的问题: 1.关关设置的问题 2.模板标签关关不支持 排除法之后,得出我的网站是模板的问 ...
- 原生js轮播以及setTimeout和setInterval的理解
下面这个代码是从一个群下载下来的,为了帮助自己理解和学习现在贴出来,与初学者共勉. <!DOCTYPE html> <html> <head> <meta c ...