In Java List and array transform each other for instance analysis

  • 2020-04-01 03:53:49
  • OfStack

This article gives an example of how to convert a List to an array in Java. Share with you for your reference. The details are as follows:

Today, I encountered a strange problem in writing the code, 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's nothing wrong with writing the code this way, and nothing wrong with compiling it. But the specific operation Exception, as follows: the Exception in the thread "is the main" Java. Lang. ClassCastException: [Ljava. Lang. Object;

But it's okay to write:


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);
}

The explanation for this is that up and down transitions are allowed in Java, but the success of this transition depends on the type of object in the Java virtual machine. The type of each object is saved in the Java virtual machine. And an array is also an object. Array type [Ljava. Lang. Object. The [Ljava. Lang. Object into a [Ljava. Lang. String is clearly impossible, because there is a downward transition, and virtual machine saved this is only an Object array, can not guarantee the elements in the array is a String, so the transition can't success. Only the inside of the array elements of reference, not storage specific elements, so the type of the elements in an array or save in the Java virtual machine.

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


Object objs[]=new Object[10];
String strs[]=(String[])objs;

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


String strs[]=new String[10];
Object objs[]=strs;

This will compile through. So this question can be boiled down to a question of Java transformation rules. Let's talk about Java array support for generics.

There is already support for generics in JDK5 to secure data types in collections and maps, but the fact that the toArray method of List returns Object [] is confusing. The individual sensation should be able to directly return the corresponding T [] according to the paradigm. A closer look at the JDK source code reveals that there are two ways to convert a List to an array:

Public Object [] toArray ();

This method returns all the elements in the List to an array of the same size, all of which are of type Object.

The public < T> T [] toArray (T [] a);

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

List is designed this way because the Java compiler does not allow us to type an array in a new paradigm. So you can't define an array like this:

T arr = new T [size];

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


public <T> T[] toArray(T[] a) {
 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 a;
}

As you can see from the above code, since you do not know the type of the array, you must create the array by reflection (the a.getclass ().getcomponenttype () method is to get the type of an array element).
Finally, the transformation from List to Array can be handled as follows:


ArrayList<String> list=new ArrayList<String>();
String[] strings = new String[list.size()];
list.toArray(strings);

Conversely, what if I want to convert an array to a List? As follows:


String[] s = {"a","b","c"};
List list = java.util.Arrays.asList(s);

I hope this article has been helpful to your Java programming.


Related articles: