How to write get and set methods for arrays

I'm a little confused. Here's my work so far.

public class CourseYear
{
    private String courseName;
    private int year;
    private String tutorName;
    private String [] moduleList;

      

ModuleList must contain 6 modules

public CourseYear()
{
    courseName = "Default";
    year = 0;
    tutorName = "Joe Bloggs";
    moduleList = new String [5];
}

      

This is where my problem is, I'm not sure how to do the massive parts:

public void addModule(Module newModule, int index)
{
    Module = newModule[0];
    Module = newModule[1];
    Module = newModule[2];
    Module = newModule[3];
    Module = newModule[4];
    Module = newModule[5];
}

      

I don't know how to make get methods

public Module getModule(int index)
{
    return Module[index];
}

      

+3


source to share


4 answers


you need to reference your array with index. In your class definition, you will need

private Module[] modules = new Module[6]; // initialize

If you want your array to contain instances Module

, the array must be an array of modules. Your class now has an array String

.

and then your add method will be

public void addModule(Module newModule, int index){
    this.modules[index] = newModule; // put the instance in the correct bucket
}

      

Note a few things:



1). You have 6 buckets in your array, so the allowed indices are 0-5. If the index in the method is addModule

out of bounds, you will get an exception.

2). addModule

expects to newModule

be an instance of a module. This way you are using addModule like

CourseYear courseYear = new CourseYear(); // create a courseyear
courseYear.addModule(new Module(), 0); // create a module and add it at index 0
courseYear.addModule(new Module(), 1); // create a module and add it at index 1
...

      

You can also use addModule

inside a class CourseYear

. Let's say you want to initialize in your constructor

public CourseYear(){
    this.addModule(new Module(), 0); // create a module and add it at index 0
    this.addModule(new Module(), 1); // create a module and add it at index 1
    ...
}

      

You must be able to find getModule

+2


source


    public class CourseYear
    {
        private String courseName;
        private int year;
        private String tutorName;
        private Module[] moduleList;

    public CourseYear()
    {
        courseName = "Default";
        year = 0;
        tutorName = "Joe Bloggs";
        moduleList = new Module[6];
    }

    public void addModule(Module newModule, int index)
    {
        moduleList[index] = newModule;
    }


    public Module getModule(int index)
    {
        return moduleList[index];
    }

      



+2


source


Two things.

1.If you want to store 6 values ​​in moduleList, you must instantiate with new String[6]

.

2. You will simplify your life by using an object of type List<String>

instead of maintaining an index, etc .:

 List<String> moduleList = new ArrayList<String>();

      

It is dynamic and easy to use.

+1


source


I am assuming that you wanted to write get and set methods for moduleList. Signatures will

public void setModuleList(String[] module);

public String[] getModuleList();

      

Once you get the list, you can get the items in the list.

0


source







All Articles