简体   繁体   中英

Setting a constructor and few objects in java

So I have this class called memoryStructure which is supposed to represent support structure for object called Student which has firstName, lastName, age and university.

This is trouble part:

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

Can someone at least go a bit through explaining what should I write here? It's my first touch with Java and I am having a bit of trouble.

Also, should student just be made as another class with his constructor and setters/getters for changing it through memoryarray in above class?

I am also supposed to make new array once currentarray size gets bigger than arraySize, it should be 2*arraySize and have all elements from previous array copied in. Is this also put in constructor?

I hope this question isn't too broad.

Most of the answers suggest you to use an ArrayList , which is a good idea but I suppose that it will defeat the purpose of your exercise which requires you to work with an array.

Can someone at least go a bit through explaining what should I write here? It's my first touch with Java and I am having a bit of trouble.

Firstly you'll have to define a constructor for this class (note that this is pseudo code).

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

Now take a look at the add method. What you need to do is to store the Student object you pass in parameter in your array. So it will roughly look like this:

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
}

Now you have a little idea about what you have to do.

Also, should student just be made as another class with his constructor and setters/getters for changing it through memoryarray in above class?

Yes it have to be another class.

I am also supposed to make new array once currentarray size gets bigger than arraySize, it should be 2*arraySize and have all elements from previous array copied in. Is this also put in constructor?

No, as I said above you should check for resizing your array when you want to add a Student in your array so it has to be in the add method and not in the constructor.

Notes:

  1. The method copyOf will be useful when you'll want to increase the size of your array.
  2. If you have a method to remove a student from the array, take a look at the arrayCopy method which will help you to resize it accordingly

epoch's answer to use a list is actually a better approach if you're not restricted to using arrays. Anyway, I hope I'm not spoiling the fun of learning the language by posting the code.

It should look something like this

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++;
}

In constructor I would do nothing (all int variables get automatically initialized with zeros, exactly what we need).

In add() method I would create the first array, if memoryArray is null . Then I would check, if the size of the array memoryArray.length less or equal to currentSize . If so we have to extend the array. You can do it like this:

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

Then you have to copy the old array into the new one using System.arraycopy() method. I will leave mastering the right arguments to you. Then you just throw the old array away and replace it the the new one

memoryArray = newMemoryArray;

then you just add the new element to the array and increment the currentSize :

memoryArray[currentSize] = student;
currentSize++;

That's it. So, your add() method looks like this:

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. The arraySize is not really needed here, since the array itself knows it length.

Instead of Array you can use ArrayList.

If you want to add via constructor code will be

public class MemoryStructure { 

 private ArrayList Students = new ArrayList();  

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

You can use ArrayList instead, like:

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

Then :

public void add(Student student) { 
    memoryArray.add(student);
}
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 {


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM