Total Java newbie array problem

rowanB

Junior Member
Nov 4, 2004
14
0
0
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;
}

 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
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.
 

VenomXTF

Senior member
May 3, 2004
341
15
81
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);
 

rowanB

Junior Member
Nov 4, 2004
14
0
0
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.