• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Total Java newbie array problem

rowanB

Junior Member
Basically, I need an idea of how I can use the addModule method to add the
user's input into the module class into an array and how I can use the
removeModule method to remove it from the array.

public class Student
{
private int[] moduleArray;
private String studentName;
private String number;
private Module mod;

public Student(String name, String studentNumber)
{
moduleArray = new int[12];
studentName = name;
number = studentNumber;

}

public void addModule(Module m)
{
mod = m;
moduleArray.add(mod);
}

public void removeModule(Module m)
{



}
public class Module
{
private String moduleCode;
private String moduleTitle;
private int semester1;
private int semester2;
private int courseworkWeighting;

public Module(String code, String title, int sem1, int sem2, int
courseworkWeight)
{
moduleCode = code;
moduleTitle = title;
semester1 = sem1;
semester2 = sem2;
courseworkWeighting = courseworkWeight;
}

 
Have you tried out your classes yet? For one, in the addModule method, the call "moduleArray.add" won't work because moduleArray is just an array of integers.

If you want, instead of making a moduleArray an fixed length array, you can use a dynamic data structure, such as a Vector. You can add and remove objects simply by calling the .add(Object) or .remove(Object) of the Vector class.

If you need to use a fixed data structure as an array, first you should probably change the datatype of moduleArray from int[] to Module[]. I suggest that for the add function, you iterate through your moduleArray and find the first empty cell and add to there. As for the remove, iterate through moduleArray, compare each to Module m; remove the one that matches.
 
Use the ArrayList class and do what Agent said. Create a Student object with name, id, etc and then you can do ArrayList L = new ArrayList; Student S = new Student(Bob); and add students by L.add(S);
 
Originally posted by: VenomXTF
Use the ArrayList class and do what Agent said. Create a Student object with name, id, etc and then you can do ArrayList L = new ArrayList; Student S = new Student(Bob); and add students by L.add(S);

Thanks, I know that'd be easier, but I'm supposed to do it using a normal array.
 
Back
Top