ArrayList<Student> alist = new ArrayList();
// object type is student
//arraylist contains student objects
Student[] arr= new Student[un.size()];
for(int i = 0; i <= alist .size(); i++){
them[i] = arr.get(i);
}
我想做的是创建一个学生数组,而不会出现数组越界异常。
最佳答案
在 Java(以及大多数语言)中,数组从零开始。如果您有一个大小为 N
的数组,索引将从 [0, N-1](N
的总大小)开始。
i <= alist .size()
应该是
i < alist .size()
↑
No "="
你为什么不简单地使用 toArray
?
关于java - 如何从数组列表创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20869559/