直接上测试代码……
import java.util.*;
class Test4
{
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
for(int x = 0; x <= 6; x++)
{
al.add(x,"hello!");
}
String[] a =(String[])al.toArray(); //为什么这句会在运行时期报异常?
}
}
异常提示:
import java.util.*;
class Test4
{
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
for(int x = 0; x <= 6; x++)
{
al.add(x,"hello!");
}
String[] a =al.toArray(new String[al.size()]); toArray方法传什么参数,你要搞清,
System.out.println(Arrays.toString(a));
}
}