public static void main(String[] args) { String[] a = {"1","2"}; System.out.println(Arrays.asList(a));
System.out.println(Arrays.asList("1","2"));//支持可变参数构造集合
}
输出:[1, 2]
具体看一下Arrays以及它的asList方法:
Arrays是java JDK中java.util包中的类,包含了很多对数组进行操作的静态方法:搜索、排序。
asList方法原型:
public static <T> List<T> asList(T... a) { return new ArrayList<>(a); }
asList支持泛型可变参数,内部调用new ArrayList<>(a)实现返回一个新的集合,因此可以传可变参数进去。
再继续grub一下,ArrayList<E>在Arrays内部也实现了一个版本,如下所示
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) { a = Objects.requireNonNull(array); } //其它代码 }
内部构造函数传入数组,内部有个final引用指向传入的数组,对转换后的集合做修改操作,原数组也会相应的变化,如下代码:
public static void main(String[] args) { String[] a = {"1","2"}; List b = Arrays.asList(a); b.set(0,"3"); System.out.print("["); for (String i:a) { System.out.print(i); } System.out.print("]"); System.out.println(b); }
Arrays.asList没有产生新数组,只是把原数组用ArrayList封装一下,增加了集合操作的一些方法,ArrayList本质也是数组,只不过封装了一些集合的操作方法。
/usr/java/jdk1.8.0_131/bin/java -javaagent:/opt/idea/lib/idea_rt.jar=43202:/opt/idea/bin -Dfile.encoding=UTF-8 -classpath /usr/java/jdk1.8.0_131/jre/lib/charsets.jar:/usr/java/jdk1.8.0_131/jre/lib/deploy.jar:/usr/java/jdk1.8.0_131/jre/lib/ext/cldrdata.jar:/usr/java/jdk1.8.0_131/jre/lib/ext/dnsns.jar:/usr/java/jdk1.8.0_131/jre/lib/ext/jaccess.jar:/usr/java/jdk1.8.0_131/jre/lib/ext/jfxrt.jar:/usr/java/jdk1.8.0_131/jre/lib/ext/localedata.jar:/usr/java/jdk1.8.0_131/jre/lib/ext/nashorn.jar:/usr/java/jdk1.8.0_131/jre/lib/ext/sunec.jar:/usr/java/jdk1.8.0_131/jre/lib/ext/sunjce_provider.jar:/usr/java/jdk1.8.0_131/jre/lib/ext/sunpkcs11.jar:/usr/java/jdk1.8.0_131/jre/lib/ext/zipfs.jar:/usr/java/jdk1.8.0_131/jre/lib/javaws.jar:/usr/java/jdk1.8.0_131/jre/lib/jce.jar:/usr/java/jdk1.8.0_131/jre/lib/jfr.jar:/usr/java/jdk1.8.0_131/jre/lib/jfxswt.jar:/usr/java/jdk1.8.0_131/jre/lib/jsse.jar:/usr/java/jdk1.8.0_131/jre/lib/management-agent.jar:/usr/java/jdk1.8.0_131/jre/lib/plugin.jar:/usr/java/jdk1.8.0_131/jre/lib/resources.jar:/usr/java/jdk1.8.0_131/jre/lib/rt.jar:/home/zw/gitProjects/user.ppdaicorp.com/3rdparty-dao/target/test-classes:/home/zw/gitProjects/user.ppdaicorp.com/3rdparty-dao/target/classes:/home/zw/.m2/repository/com/dangdang/sharding-jdbc-config-spring/1.5.4/sharding-jdbc-config-spring-1.5.4.jar:/home/zw/.m2/repository/com/dangdang/sharding-jdbc-config-common/1.5.4/sharding-jdbc-config-common-1.5.4.jar:/home/zw/.m2/repository/org/codehaus/groovy/groovy/2.4.5/groovy-2.4.5-indy.jar:/home/zw/.m2/repository/org/springframework/spring-context-support/4.2.3.RELEASE/spring-context-support-4.2.3.RELEASE.jar:/home/zw/.m2/repository/com/dangdang/sharding-jdbc-core/1.5.4/sharding-jdbc-core-1.5.4.jar:/home/zw/.m2/repository/com/google/guava/guava/18.0/guava-18.0.jar:/home/zw/.m2/repository/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar:/home/zw/.m2/repository/com/alibaba/druid/1.1.4/druid-1.1.4.jar:/usr/java/jdk1.8.0_131/lib/jconsole.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:/home/zw/.m2/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar:/home/zw/.m2/repository/org/mybatis/mybatis/3.3.1/mybatis-3.3.1.jar:/home/zw/.m2/repository/org/mybatis/mybatis-spring/1.2.4/mybatis-spring-1.2.4.jar:/home/zw/.m2/repository/com/github/pagehelper/pagehelper/4.1.6/pagehelper-4.1.6.jar:/home/zw/.m2/repository/com/github/jsqlparser/jsqlparser/0.9.5/jsqlparser-0.9.5.jar:/home/zw/.m2/repository/org/springframework/spring-context/4.2.3.RELEASE/spring-context-4.2.3.RELEASE.jar:/home/zw/.m2/repository/org/springframework/spring-aop/4.2.3.RELEASE/spring-aop-4.2.3.RELEASE.jar:/home/zw/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar:/home/zw/.m2/repository/org/springframework/spring-beans/4.2.3.RELEASE/spring-beans-4.2.3.RELEASE.jar:/home/zw/.m2/repository/org/springframework/spring-core/4.2.3.RELEASE/spring-core-4.2.3.RELEASE.jar:/home/zw/.m2/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar:/home/zw/.m2/repository/org/springframework/spring-expression/4.2.3.RELEASE/spring-expression-4.2.3.RELEASE.jar:/home/zw/.m2/repository/org/springframework/spring-tx/4.2.3.RELEASE/spring-tx-4.2.3.RELEASE.jar:/home/zw/.m2/repository/org/springframework/spring-jdbc/4.2.3.RELEASE/spring-jdbc-4.2.3.RELEASE.jar:/home/zw/gitProjects/user.ppdaicorp.com/3rdparty-domain/target/classes:/home/zw/.m2/repository/com/alibaba/fastjson/1.2.8/fastjson-1.2.8.jar:/home/zw/.m2/repository/junit/junit/4.12/junit-4.12.jar:/home/zw/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/home/zw/.m2/repository/org/springframework/spring-test/4.2.3.RELEASE/spring-test-4.2.3.RELEASE.jar com.ppdai.cbd.thirdparty.dao.mapper.Test[1, 2]