In Java, list is converted to array and array to list.

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/zhqingyun163/archive/2009/10/29/4744365.aspx

Write todayCodeA strange problem occurs. The specific code is not pasted out, so you can write a simplified version. As follows:

Arraylist <string> List = new arraylist <string> ();

String strings [] = (string []) List. toarray ();

In this way, I personally think there should be no problem in writing code, and there is no problem in compiling. However, an exception is reported during running, as follows: exception in thread "Main" Java. Lang. classcastexception: [ljava. Lang. object;

But there is no problem with this writing:

Arraylist <string> List = new arraylist <string> ();

String strings [] = new string [list. Size ()];

For (INT I = 0, j = List. Size (); I <j; I ++ ){

Strings [I] = list. Get (I );

}

This can be explained as follows: Java allows upward and downward transformation, but whether the transformation is successful depends on the type of the object in the Java Virtual Machine. The Java virtual machine saves the type of each object. Array is also an object. The array type is [ljava. Lang. object. Set [ljava. lang. convert an object to [ljava. lang. string is obviously impossible, because here is a downward transformation, and the virtual machine only saves this is an array of objects, the elements in the array cannot be guaranteed to be string, so this transformation cannot be successful. The elements in the array are only element references, not specific storage elements. Therefore, the element types in the array are stored in the Java Virtual Machine.

According to the above explanation, we can summarize this problem into the following model:

Object objs [] = new object [10];

String STRs [] = (string []) objs;

This is the same as the preceding compilation error. If we modify the Code as follows:

String STRs [] = new string [10];

Object objs [] = STRs;

In this way, the compilation is successful. Therefore, this problem can be attributed to a Java transformation rule. Next, let's talk about the support for the Java array model.

Jdk5 already has support for the model, which ensures the security of the data types in the set and map. However, the toarray method of list returns object [], which is confusing. I personally feel that the corresponding T [] can be directly returned based on the model. After carefully reading the JDK source code, we found that there are two ways to convert list to array:

Public object [] toarray ();

This method returns all elements in the list to an array of the same size. All elements in the array are of the object type.

Public <t> T [] toarray (T [] );

This method returns all elements in the list to an array of the same size. All elements in the array are of the T type.

The list is designed so that the Java compiler does not allow us to use new arrays. That is to say, you cannot define an array like this:

T arr = new T [size];

However, you can use t [] to represent arrays and forcibly convert arrays to T []. For example, the Public <t> T [] toarray (T [] A) in the list is implemented as follows:

Public <t> T [] toarray (T [] ){

If (A. Length <size)

A = (T []) Java. Lang. Reflect. array.

Newinstance (A. getclass (). getcomponenttype (), size );

System. arraycopy (elementdata, 0, A, 0, size );

If (A. length> size)

A [size] = NULL;

Return;

}

As shown in the code above, because you do not know the type of this array, you must use the reflection mechanism to create this array (. getclass (). the getcomponenttype () method is used to obtain the type of an array element ).

Finally, the list can be converted to array as follows:

Arraylist <string> List = new arraylist <string> ();

String [] strings = new string [list. Size ()];

List. toarray (strings );

What if we want to convert the array into a list? As follows:

String [] S = {"A", "B", "C "}; 


List list = java. util. arrays. aslist (s );


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.