所以,我有这个代码:
public void add(Student student) {
if(currentSize == arraySize){
arraySize *= 2;
Student[] pomArray = memoryArray;
memoryArray = new Student[arraySize];
for(int i = 0; i < currentSize; i++){
memoryArray[i] = pomArray[i];
}
}
memoryArray[currentSize] = student;
currentSize++;
基本上,如果 currentSize
变得与 arraySize
一样大,我需要将新数组设置为两倍大,并复制其中的所有数据。我不明白的是这个新数组的制作方式。
我会这样做:
student[] pomArray = new Student[arraySize]
或者这些线之间的东西。
我不明白 student[] pomArray = memoryArray
是什么意思。
还有memoryArray[Currentsize] = Student
行。
如果你们能引导我到一个有很好解释的地方,那就最好了,我找不到任何东西。
最佳答案
我认为如果您使用集合会更容易,除了集合类提供比数组更有用的方法之外,它们还会随着您添加和删除条目而扩展和减少。例如:
//Importing Collections
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
//use this to create arraylist
List<Student> Studentlist = new ArrayList<Student>();
Student Student1 = new Student();
Studentlist.add(Student1);
With this you need not bother about expanding;
关于java - java中的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22479109/