java - java中的对象数组

标签 java arrays object

所以,我有这个代码:

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/

相关文章:

java - Android Studio findViewById(R.id.button1) 错误

java - 用新的 WebClient API 替换 Spring KerberosRestTemplate

c - 将数组作为参数传递给 C 中的函数

ios - Core Data 删除同名对象

c# - 如何从 XML 文件创建可序列化的 C# 类

java - Spring MVC 使用 Get 请求绑定(bind)命令对象

java - 异常 java.security.InvalidKeyException : Invalid key length: 24 bytes using DESede

java - 数组约束满足

python - 在python中使用迭代器对数组求和

javascript - JS 在对象数组上使用indexOf