Java ToArray (new string[0])

Source: Internet
Author: User
Tags java se

In the process of looking at the code, see this usage

Fnlist.toarray (new string[0])

Very confused, do not know what this is doing, on the Internet search the following.

http://rogerfederer.iteye.com/blog/788795 public <T> t[] ToArray (t[] a) {if (A.length < size) A = (t[)) java.la Ng.reflect.Array. Newinstance (A.getclass (). Getcomponenttype (), size); System.arraycopy (elementdata, 0, a,0, size); if (a.length > Size) a[size] = null; return A; Public object[] ToArray () {Object aobj[] = new Object[size]; System.arraycopy ((object) (Elementdata), 0, ((object) (aobj)), 0, size); return aobj; Public object[] ToArray (Object aobj[]) {if (Aobj.length < size) Aobj = (object[]) (object[)) Array.newinstance ((objec T) (Aobj)). GetClass (). Getcomponenttype (), size); System.arraycopy ((object) (Elementdata), 0, ((object) (aobj)), 0, size); if (aobj.length > Size) aobj[size] = null; return aobj; }

ToArray () and ToArray (t[] a) method "> public <T> t[] ToArray (t[] a) 



1. The method is generic and is used in the creation of the method (<T> is equivalent to defining generics, t[] is using generic T)
Generics are a new feature of the Java SE 1.5, the essence of which is a parameterized type, which means that the data type being manipulated is specified as a parameter. This type of parameter can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, generic methods, respectively.
2. This method returns an array of all elements in the collection, and the Run-time type of the array returned is the same as the run-time type of the specified array.


3. Comparison with public object[] ToArray ()
Public object[] ToArray () {
Object[] result = new Object[size];
System.arraycopy (elementdata, 0, result, 0, size);
return result;
}

From the source can be seen that it can only return object[] type, equivalent to ToArray (new object[0]) Note: Arrays cannot be cast

ToArray method without parameters, is an object array constructed, and then copy the data, this time the transformation will produce classcastexception

String[] tt = (string[]) List.toarray (new string[0));


This code is OK, but we see that the parameters in string[] tt = (string[]) List.toarray (new string[0]) are very strange, but removing this parameter new String[0] is wrong in the run-time times ...

The elements in the container have been restricted by generics, and the elements should be viewed as generic types, whereas in current Java it is not, when directly string[] TT = (string[]) List.toarray (), the error is run.

Recall that the mandatory type conversions in Java are only for a single object, and it is not possible to get lazy to convert the entire array into an array of another type, which is similar to the initialization of the arrays.



The ToArray method with parameters, based on the type of the parameter array, constructs an empty array of the corresponding type, which is consistent with the size of the ArrayList, although the method itself returns the result as an object array, However, because the componenttype of the constructed array is consistent with the componenttype that needs to be transformed, no transition anomaly is produced.




Solution. Solutions

So in the use of ToArray can refer to the following three ways

1. long[] L = new Long[<total size>];

List.toarray (l);

2. long[] L = (long[]) List.toarray (new long[0));

3. long[] A = new Long[<total size>];

Long[] L = (long[]) List.toarray (a);


1. Parameters specify an empty array, save space
string[] y = X.toarray (new string[0]);
2. Specify large Array parameters waste time, using reflection mechanism
string[] y = X.toarray (new string[100]); Assuming the array size is greater than 100
3). Consider the best
string[] y = X.toarray (new String[x.size ()));


The following code will appear classcastexception
List List = new ArrayList ();
List.add (New Long (1));
List.add (New Long (2));
List.add (New Long (3));
List.add (New Long (4));
Long[] L = (long[]) List.toarray ();//This statement appears classcastexception


The processing method is as follows:
Long [] L = (long []) List.toarray (new Long[list.size ());

http://blog.csdn.net/jrq/article/details/517428

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.