在java中设置构造函数和少量对象

时间:2014-03-10 08:42:17

标签: java arrays

所以我有一个名为memoryStructure的类,它应该代表名为Student的对象的支持结构,该对象具有firstName,lastName,age和university。

这是麻烦部分:

public class MemoryStructure { 

    private Student[] memoryArray;  
    private int currentSize; 
    private int arraySize; 

    // constructor goes here 

    /** 
     * Adds the object <code>student</code> to the collection right after the 
     * last element in the current collection. 
     * 
     * @param student 
     * Object to be added to the collection.
     */ 

    public void add(Student student) { 

    // to be done

有人至少可以解释一下我应该在这里写些什么吗? 这是我第一次使用Java,我遇到了一些麻烦。

另外,是否应该让学生作为另一个班级,使用他的构造函数和setter / getter来通过上面的类中的memoryarray来改变它?

一旦currentarray的大小比arraySize大,我也应该创建新的数组,它应该是2 * arraySize并且复制了之前数组中的所有元素。这也放在构造函数中吗?

我希望这个问题不是太宽泛。

6 个答案:

答案 0 :(得分:1)

大多数答案建议您使用ArrayList,这是一个好主意,但我想它会破坏您需要使用数组的练习目的。

  

有人至少可以解释我应该写什么   这里?这是我第一次使用Java,我遇到了一些麻烦。

首先,你必须为这个类定义一个构造函数(注意这是伪代码)。

MemoryStructure (size) {
   define the arraySize
   create the array with this size
   initialize the currentSize to 0 
}

现在看一下add方法。您需要做的是将您传递的Student对象存储在数组中的参数中。所以它大致如下:

public void add(Student student) {
    if(currentSize == arraySize)
         increase the capacity of the array
    storeStudent in the array at the right index (you can use currentSize since arrays are 0 base indexed)
    increment currentSize
}

现在你对自己要做的事情有了一点了解。

  

另外,学生应该和他一起成为另一个班级   构造函数和setter / getters通过memoryarray来改变它   上课?

是的,它必须是另一个班级。

  

我也应该在currentarray大小获得后创建新数组   大于arraySize,它应该是2 * arraySize并具有所有元素   从上一个数组复制而来。这是否也放在构造函数中?

不,正如我上面所说的那样,当你想在数组中添加一个Student时,你应该检查你的数组的大小,这样它就必须在add方法中,而不是在构造函数中。

注意:

  1. 如果您想增加数组的大小,方法copyOf会很有用。
  2. 如果您有从阵列中删除学生的方法,请查看arrayCopy方法,以帮助您相应地调整大小。

答案 1 :(得分:1)

如果您不限制使用数组,那么epoch使用列表的答案实际上是一种更好的方法。无论如何,我希望通过发布代码来破坏学习语言的乐趣。

看起来应该是这样的

public void add(Student student) {
   // assuming currentSize = 0 and arraySize = *something* in the constructor and that    memoryArray = new Student[*something*]
   if(currentSize == arraySize){
      arraySize = arraySize*2;
      memoryArray = Arrays.copyOf(memoryArray, arraySize);
   }
   memoryArray[currentSize] = student;
   currentSize++;
}

答案 2 :(得分:1)

在构造函数中我什么都不做(所有int变量都会自动用零填充,正是我们需要的。)

add()方法中,如果memoryArraynull,我会创建第一个数组。然后我会检查,如果数组memoryArray.length的大小小于或等于currentSize。如果是这样,我们必须扩展数组。你可以这样做:

Student[] newMemoryArray = new Student[memoryArray.length * 2];

然后,您必须使用System.arraycopy()方法将旧数组复制到新数组中。我将离开掌握正确的论据给你。然后你只需抛弃旧阵列并将其替换为新阵列

memoryArray = newMemoryArray;

然后你只需将新元素添加到数组中并递增currentSize

memoryArray[currentSize] = student;
currentSize++;

就是这样。因此,您的add()方法如下所示:

public void add(Student student) { 
    if (memoryArray == null) {
        memoryArray = new Student[10];
    }
    else if (memoryArray.length <= currentSize) {
        Student[] newMemoryArray = new Student[memoryArray.length * 2];
        // Copy old array into the new one
        memoryArray = newMemoryArray;
    }

    memoryArray[currentSize] = student;
    currentSize++;
}

PS。这里并不真正需要arraySize,因为数组本身知道它的长度。

答案 3 :(得分:0)

您可以使用ArrayList而不是Array。

如果要通过构造函数添加代码将

public class MemoryStructure { 

 private ArrayList Students = new ArrayList();  

 MemoryStructure(Student student){
          Students.add(student);
        }
}

答案 4 :(得分:0)

您可以使用ArrayList,例如:

ArrayList<Student> memoryArray = new ArrayList<Student>();

然后:

public void add(Student student) { 
    memoryArray.add(student);
}

答案 5 :(得分:0)

import java.util.ArrayList;
import java.util.List;


public class MemoryStructure {

    private static List<Student> memoryArray; 

    public void add(Student student) { 
        if(memoryArray ==null) {
            memoryArray = new ArrayList<>();
        }
        memoryArray.add(student);
    }

    public int getCurrentSize() {
        if(memoryArray ==null) {
            return 0;
        }
        return memoryArray.size();
    }


}

public class Student {


}