如题,直接进入主题了,一个测试代码,一个输出日志,配合上该方法的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
分享到:
相关推荐
NULL 博文链接:https://jiaguwen123.iteye.com/blog/1679345
之前一直认为因为List内部实现是数组,ToArray的实现只是将数组返回出去而已。 今天测了一下发现并不是那样 var a = new List(); for (int i = 0; i < 10000; i++) { a.Add(i); } DebugHelper.StartWatch(); ...
jquery.toArray()是将jQuery对象转换成数组,我们来分析下
本文实例讲述了C#中List和数组之间转换的方法。分享给大家供大家参考。具体分析如下: 一、List转数组 (从List转到string[]) List<string> listS=new List(); listS.Add(str); listS.Add(hello); string[] str=...
Map js文件为 js/utils.js IE6.0 测试通过 <br>List: add(var obj) //添加一个元素 remove(var index) //删除一个元素 get(var index) //获取一个元素 removeAll() //删除所有元素 toArray...
(81条消息) GEE(Google Earth Engine) ee_List_sequence、ee_image_toArray(axis)和协方差矩ee_Reducer_centeredCovariance()_此星光明的博客-CSDN博客.mht
主要介绍了Java ArrayList.toArray(T[]) 方法的参数类型是 T 而不是 E的原因分析的相关资料,需要的朋友可以参考下
今天小编就为大家分享一篇解决Laravel5.5下的toArray问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
代码如下:jQuery.fn = jQuery.prototype = { constructor: jQuery, init: function( selector, context, rootjQuery ) { }, selector: “”, jquery: “1.7.2”, length: 0, size: function() {}, toArray: ...
List<T> duplicates = new List( ); for (int i = 0; i < this.Count; i++) { if (this[i].Equals(inputValue)) { duplicates.Add(this[i]); } } return (duplicates.ToArray( )); } 希望本文所述对大家...
这个纯手工记录的全套php笔记,楼主还是很有信心的,笔记每天都会看着敲一下长进真的不少,期待自己有一天成为传说中的"php大牛"。
rest $List.prototype.length $List.prototype.toString $List.prototype.reverse $List.prototype.slice $List.prototype.ref $List.prototype.append $List.prototype.toArray $List.prototype.forEach $List....
List = require ( 'leetcode' ) . List ; 列表节点 ListNode构造函数,用于在列表中创建节点。 ListNode.prototype.toArray 将列表转换为数组。 通常用于调试。 var List = require ( 'leetcode' ) . List ; // { ...
_.toArray(list) Converts the list (anything that can be iterated over), into a real Array. Useful for transmuting the arguments object. _.size(list) Return the number of values in the list.
TextBox控件重新封装(源代码网上找的),自己重新封装方便使用; 使用步骤: 1,加载dll文件;... return list.ToArray(); } 4,数据源绑定: spellSearchBoxExSS1.SpellSearchSource = GetSpellBoxSource(dt);
list.toArray(array1); 3.如果列表中的元素是可比较大小的,则可用Java.util.Collections类中的静态方法sort(列表)方法进行排序 Collections.sort(list); System.out.println(list); ;4.求最大最小值 Collections...