Analysis of the conversion between list and array in Java

This paper analyzes the method of converting list and array in Java. Share with you for your reference. The details are as follows:

Today, I encountered a strange problem when writing code. The specific code will not be posted. Write a simplified version. As follows:

I think there should be no problem writing code like this, and there should be no problem compiling it. However, exceptions are reported during specific operation, as follows: exception in thread "main" Java lang.ClassCastException: [Ljava.lang.Object;

But there is no problem with this:

We can explain this phenomenon as follows: upward and downward transformation is allowed in Java, but the success of this transformation is realized according to the type of this object in the Java virtual machine. The type of each object is saved in the Java virtual machine. The array is also an object. Array type [ljava. Lang. object]. Convert [ljava. Lang. object to [ljava.lang.string is obviously impossible, because it is a downward transformation, and the virtual machine only saves an array of objects. The elements in the array cannot be guaranteed to be string, so the transformation cannot succeed. The elements in the array are only the reference of elements, not the stored specific elements, so the types of elements in the array are still saved in the Java virtual machine In.

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

This is the same as the above compilation error. If we modify this code, it is as follows:

In this way, the sub can be compiled. So this problem can be reduced to a problem of Java transformation rules. Let's talk about the support of Java arrays for templates.

Jdk5 already has support for templates, which can ensure the security of data types in collections and maps. However, it is very confusing that the toArray method of list returns object []. Personal feeling should be able to directly return to the corresponding t according to the paradigm []. After a closer look at the source code of JDK, it is found that there are two methods to convert list into 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 type object.

public
T[] toArray(T[] a);

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

List is designed this way because the java compiler does not allow us to use the new paradigm array. In other words, you can't define an array like this:

T arr=new T[size];

But you can use t [] to represent an array, and you can force the array into t []. For example, public < T > t [] toArray (t [] a) in list is implemented as follows:

As you can see from the above code, because you don't know the type of this array, you must create this array through reflection mechanism (a.getclass() The getcomponenttype () method gets the type of an array element. Finally, the conversion from list to array can be handled as follows:

Conversely, what if you want to turn an array into a list? As follows:

I hope this article will be helpful to your Java programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>