To get a better understanding, let's look at the following example:
Package com.yonyou.test;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
/**
* Test class
* @author cloud-dwelling Community
* @ Creation Date
2016-5-31
/public class test{public
static void main (String [] args) {
list<string> list=new arraylist<string> ();
List.add ("Hello");
List.add ("World");
List.add ("Hahahaha");
String[] Strarray2=new string[list.size ()];
List.toarray (strArray2);
for (int i=0;i<strarray2.length;i++)
{
System.out.println (strarray2[i]);
}
The following will cause the cast error
string[] strarray= (string[]) List.toarray (); List.toarray () returns an array of object[] types that you want to cast to string[] for
(int i=0;i<strarray.length;i++)
{
System.out.println (Strarray[i]);}}
The above article on the forced type conversion in Java is the small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.