This article analyzes the method of changing List and array in Java. Share it for everyone for your reference. The specifics are as follows:
Today I wrote the code to encounter a strange problem. The specific code is not posted. Write a simplified version. as follows:
ArrayList <string> list = new arraylist <string> (); string strings [] = (string []) list.toarray ();
I think there should be no problem in writing code in this way, and there is no problem compilation. However, when the specific operation is running, the report is abnormal, as follows: Exception in Thread "Main" Java.lang.ClassCastexception: [ljava.lang.object;
But there is no problem writing like this:
ArrayList <string> list = New ArrayList <string> (); String Strings [] = New String [list.size ()]; for (int i = 0, j = list.size (); {strings [i] = list.get (i);}
We can explain this phenomenon: Java allows upward and downward transformation, but whether this transformation is successful based on the type of this object in the Java virtual machine. The Java virtual machine saves the type of each object. The array is also an object. The type of array [ljava.lang.object. Converting [ljava.lang.object to [ljava.lang.string is obviously impossible, because this is a downward transformation, and the virtual machine only preserves that this is an Object array. It cannot guarantee that the element in the array is the element in the array is the element is the element in the array. String, so this transformation cannot be successful. The elements in the array are only the references of the element, not the specific element of storage, so the type of element in the array is still stored in the Java virtual machine.
According to the above explanation, we can summarize this problem to the following model:
Object objs [] = new object [10]; string strs [] = (string []) objs;
This is the same as the above compilation. If we modify this code, as follows:
String strs [] = New String [10]; Object Objs [] = strs;
This can be compiled and passed. So we can attribute this problem to a problem of a Java transformation rule. Let's talk about the support of the Java array on the model.
JDK5 already has support for model types, which can ensure the security of data types in the collection and MAP. However, the List's Toarray method returns to Object [] very confusing. Personally, I should return the corresponding T [] according to the model. Take a closer look at the source code of JDK and find that List has two ways to transform into Array:
public object [] toarray ();
This method returns all elements in List to an array of the same size, and all elements in the array are Object type.
public <t> t [] toarray (t [] a);
This method returns all elements in List to a array of the same size, and all elements in the array are T type.
List is designed because the Java compiler does not allow us to have new model arrays. That is to say, you can't define an array like this:
T arr = new t [size];
But you can use T [] to represent the array, and you can convert the array to T []. For example, the Public <T> t [] toarray (t [] a) in List is realized:
public <t> t [] toarray (t [] a) {if (a.Length <size) a = (t []) java.lang.reflect.array.newinstance (a.getClass (). GetComponenttype (), GetComponenttype (), size); System.arrayCopy (ElementData, 0, A, 0, SIZE); if (a.Length> size) a [size] = null; return a;}
It can be seen from the above code that because you don't know the type of this array, you must create this array through the reflection mechanism (A.GetClass (). GetComponenttype () method to obtain a type of array element).
In the end, List converts to array to handle it::
ArrayList <string> list = new arraylist <string> (); string [] strings = new string [list.size ()]; list.toarray (strings);
Conversely, what if you want to turn the array into list? as follows:
String [] s = {"a", "b", "c"}; list list = java.util.arrays.aslist (s);
It is hoped that this article is helpful to everyone's Java program design.