Java's container class collections ToArray () method, you can put a dynamic array such as arraylist<string>, variable length conversion static array, fixed-length array string[]
However, the following conversion method is wrong.
String[] Strarray = (string[]) Arraylist.toarray ();
If this execution causes the following error:
Exception in thread "XX" java.lang.ClassCastException: [Ljava.lang.Object; Cannot is cast to [Ljava.lang.String;
Such as:
The right way should be written as:
String[] Strarraytrue = (string[]) Arraylist.toarray (new string[0]);
For example, the following procedure is normal:
Import java.util.*;p Ublic class Arraylisttostrarr {public static void main (string[] args) {arraylist<string> ArrayList = new arraylist<string> () Arraylist.add ("1"); Arraylist.add ("2");//correct conversion mode string[] Strarraytrue = ( String[]) Arraylist.toarray (new string[0]); for (String str:strarraytrue) {System.out.println (str);} Wrong way of conversion//string[] Strarray = (string[]) Arraylist.toarray ();}}
Similarly extended to array arraylist<integer> etc.
"Java" arraylist<string> conversion to String array problem