Java Container Class Collections ToArray () method, can transform such as arraylist<string> dynamic array, indefinite length of static array, fixed-length array string[]
However, the conversion method is wrong.
String[] Strarray = (string[]) Arraylist.toarray ();
If this execution results in the following error:
Exception in thread "XX" java.lang.ClassCastException: [Ljava.lang.Object; cannot being cast to [Ljava.lang.String;
The following figure:
The correct way should be written:
String[] Strarraytrue = (string[]) Arraylist.toarray (new string[0));
For example, the following procedure is normal:
Import java.util.*;
public 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);
}
Incorrect conversion mode
//string[] Strarray = (string[]) Arraylist.toarray ();
}
Similarly extended to array arraylist<integer>, etc.