`
daijun74
  • 浏览: 48539 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

List toArray方法学习笔记

    博客分类:
  • Java
 
阅读更多

如题,直接进入主题了,一个测试代码,一个输出日志,配合上该方法的JavaDoc

 

1.测试代码

public static void main(String[] args) {
		List<String> strList = new ArrayList<String>();
		String tempStr = "str ";
		for (int i = 0; i < 10; i++) {
			strList.add(tempStr + i);
		}

		String[] tempArr = new String[15];
		tempArr[0] = "tempStr0";
		
		String[] tempArr2 = new String[15];
		String[] tempArr3 = new String[0];

		String[] strList2Arr = strList.toArray(tempArr);
		String[] strList2Arr2 = strList.toArray(tempArr2);
		String[] strList2Arr3 = strList.toArray(tempArr3);

		System.out.println("============================================");
		System.out.println("strList2Arr = " + strList2Arr);
		System.out.println("tempArr = " + tempArr);

		System.out.println("============================================");
		System.out.println("strList2Arr2 = " + strList2Arr2);
		System.out.println("tempArr2 = " + tempArr2);

		System.out.println("============================================");
		System.out.println("strList2Arr3 = " + strList2Arr3);
		System.out.println("tempArr3 = " + tempArr3);

		printStringArray("strList2Arr", strList2Arr);
		printStringArray("strList2Arr2", strList2Arr2);
		printStringArray("strList2Arr3", strList2Arr3);

		Integer[] intList2Arr3 = strList.toArray(new Integer[0]); // test java.lang.ArrayStoreException
	}

	private static void printStringArray(String arrName, String[] arr) {
		System.out.println("============================================");
		System.out.println(arrName);
		for (String str : arr) {
			System.out.print(str + "  ");
		}
		System.out.println();
	}

 2.输出日志

============================================
strList2Arr = [Ljava.lang.String;@3b061299
tempArr = [Ljava.lang.String;@3b061299
============================================
strList2Arr2 = [Ljava.lang.String;@baf1915
tempArr2 = [Ljava.lang.String;@baf1915
============================================
strList2Arr3 = [Ljava.lang.String;@1497b7b1
tempArr3 = [Ljava.lang.String;@749cd006
============================================
strList2Arr
str 0  str 1  str 2  str 3  str 4  str 5  str 6  str 7  str 8  str 9  null  null  null  null  null  
============================================
strList2Arr2
str 0  str 1  str 2  str 3  str 4  str 5  str 6  str 7  str 8  str 9  null  null  null  null  null  
============================================
strList2Arr3
str 0  str 1  str 2  str 3  str 4  str 5  str 6  str 7  str 8  str 9  
Exception in thread "main" java.lang.ArrayStoreException
	at java.lang.System.arraycopy(Native Method)
	at java.util.Arrays.copyOf(Unknown Source)
	at java.util.ArrayList.toArray(Unknown Source)
	at com.dj.generics.Test.main(Test.java:44)

 3.java.util.List#toArray方法的JavaDoc

<String> String[] java.util.List.toArray(String[] a)

toArray
<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list. 
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.) 

Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs. 

Suppose x is a list known to contain only strings. The following code can be used to dump the list into a newly allocated array of String: 

     String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to toArray().
Specified by: 
toArray in interface Collection<E> 
Parameters:
a - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose. 
Returns:
an array containing the elements of this list 
Throws: 
ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list 
NullPointerException - if the specified array is null

 

小结:

1.toArray()直接返回Object[],没有用到泛型概念,需要程序员在代码中进行类型转换;

2.toArray(T[] a)引入泛型概念,以用户指定类型的数组进行转换;如果用户调用toArray时传入的数组大小足以容纳list内的元素,那么编译器将会清空该数组,并把list内数据按照原有顺序存放到该数组内,该数组内剩余的存储空间都会被分配null(仿佛这种用法比较少);而如果用户传入的数组大小不足以容纳list内的元素,编译器将会在运行时以list的size创建一个和用户传入参数一样数据类型的数组,并返回(常见到调用toArray时直接新建一个length=0的数组);

3.如果传入的数组的数据类型和list内实际元素的数据类型不相符合,将会在运行时抛出ArrayStoreException

 

-over 

 

分享到:
评论
1 楼 u010214546 2015-06-17  
不错!文档中的(This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)   是啥意思,没看太明白。

相关推荐

Global site tag (gtag.js) - Google Analytics