参考文章:
http://www.cnblogs.com/zolra/archive/2011/03/02/1969235.html
http://blog.csdn.net/anders_zhuo/article/details/8960996
原文地址:
http://blog.csdn.net/love_xsq/article/details/43698903
package se;
import java.util.Arrays;
import java.util.List;
/**
*
* 本类演示了Arrays类中的asList方法
* 通过四个段落来演示,体现出了该方法的相关特性.
*
* (1) 该方法对于基本数据类型的数组支持并不好,当数组是基本数据类型时不建议使用
* (2) 当使用asList()方法时,数组就和列表链接在一起了.
* 当更新其中之一时,另一个将自动获得更新。
* 注意:仅仅针对对象数组类型,基本数据类型数组不具备该特性
* (3) asList得到的数组是的没有add和remove方法的
*
* 阅读相关:通过查看Arrays类的源码可以知道,asList返回的List是Array中的实现的
* 内部类,而该类并没有定义add和remove方法.另外,为什么修改其中一个,另一个也自动
* 获得更新了,因为asList获得List实际引用的就是数组
*/
public class AsListTest {
public static void main(String[] args) {
/* 段落一:基本数据类型使用asList中的问题 */
/* 说明:虽然在JDK1.6中能够将基本数据类型的数组转换成List,但还是有个缺陷 */
int[] a_int = { 1, 2, 3, 4 };
/* 预期输出应该是1,2,3,4,但实际上输出的仅仅是一个引用, 这里它把a_int当成了一个元素 */
List a_int_List = Arrays.asList(a_int);
foreach(a_int_List);
/* 为此我们需要这样遍历其中元素 */
foreachForBase(a_int_List);
/* 段落二:对象类型的数组使用asList,是我们预期的 */
Integer[] a_Integer = new Integer[] { 1, 2, 3, 4 };
List a_Integer_List = Arrays.asList(a_Integer);
foreach(a_Integer_List);
/* 段落三:当更新数组或者asList之后的List,另一个将自动获得更新 */
a_Integer_List.set(0, 0);
foreach(a_Integer_List);
foreach(a_Integer);
a_Integer[0] = 5;
foreach(a_Integer_List);
foreach(a_Integer);
/* 段落四:对基本类型数组,通过asList之后的List修改对应的值后,在运行时会报出异常
* 但是基本类型数组对应的List是会发生变化的,这是毫无疑问的
*/
/*
* a_int_List.set(0, 0);
* foreach(a_int_List);
* foreach(a_int);
*/
a_int[0] = 6;
foreachForBase(a_int_List);
foreach(a_int);
}
/* 打印方法 */
private static void foreach(List list) {
for (Object object : list) {
System.out.print(object + " ");
}
System.out.println();
}
private static void foreachForBase(List a_int_List) {
//基本数据类型数组使用aslist只有一个元素,使用get(0)获取,可以将其转换成基本数组
int[] _a_int = (int[]) a_int_List.get(0);
foreach(_a_int);
}
private static void foreach(int[] a_int) {
for (int i : a_int) {
System.out.print(i + " ");
}
System.out.println();
}
private static void foreach(Integer[] _a_Integer) {
for (int i : _a_Integer) {
System.out.print(i + " ");
}
System.out.println();
}
}
Arrays.copyOfRange源代码
public static short[] copyOfRange(short[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
short[] copy = new short[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
注意点:复制长度 Math.min(original.length - from, newLength)
package Test;
import java.util.Arrays;
/**
* https://www.tutorialspoint.com/java/util/arrays_copyofrange_short.htm
* */
public class ArrayDemo {
public static void main(String[] args) {
// intializing an array arr1
short[] arr1 = new short[] {0,1,2,3,4,5,6,7,8,9};
// printing the array
System.out.println("Printing 1st array:");
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);
}
// copying array arr1 to arr2 with range of index from 1 to 3
short[] arr2 = Arrays.copyOfRange(arr1, 8, 15);
// printing the array arr2
System.out.println("Printing new array:");
for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
}
}