Jason Jason - 3 years ago 123
Java Question

Accessing Objects from ArrayLists

I have an arraylist of objects that I am sending from class "Testing" to class "Subjects", but can't access some of the methods in the Subjects class.

I would like to be able to simply send an arraylist of "Subject" objects (from the Testing class) to a method in the Subject class and be able to use other methods within the Subject class.

public class Subject {
private String subjectName;
private String courseCode;

//getters and setters for subjectName and courseCode


I can access the following method if I just call it from the Subject class, but not from the Testing class.

public String getDiscipline(){
int a = courseCode.length()-3;
String discipline = courseCode.substring(0, a);
return discipline;
}


In the method below, I can return and print objects and call the method I want, but only for one object...

public List<?> allDisciplines(ArrayList<?> inputSubjects){

for (int i = 0; i<inputSubjects.size(); i++){
System.out.println(inputSubjects.get(i)); //this returns all the

//objects, but why won't this work instead?: inputSubjects.get(i).getDiscipline();

}

//initialise new arraylist
List<String> dis = new ArrayList<>();

dis.add(getDiscipline()); //This works, but only for whatever single
//object I used to call the allDisciplines
//method with
//

return dis;
}


In another class , I pass the object array "subjects" into the allDisciplines method of the Subject class.

public class Testing {
public static void main(String[] args) {
// TODO code application logic here
Testing program = new Testing();
program.start();
}

public void start(){
//gets the data from a file, converts it into "Subject" objects with
//subjectName and courseCode


The following works because it's only sending one object through and I only get one result.

subjects.get(3).allDisciplines(subjects);


But if I try to access the allDisciplines method from the Testing class, I can't because it's not static and if I make the class static, it needs the private ints to be static which then don't change between iterations of my loops and I end up with the same value for fields.

allDisciplines(subjects); //does not work - can't see the allDisciplines
//method


I am not sure whether the problem is because I can't access the method outside of its class or because I am not properly referencing the objects in the arraylist.

Thanks for any help you can give me - I am quite confused!

Answer Source

I think the problem is the allDisciplines method. It should be static. You seem to not quite understand generics. I rewrote it for you:

public static List<String> allDisciplines(ArrayList<Subject> inputSubjects){


    //initialise new arraylist
    List<String> dis = new ArrayList<>();

    for (Subject subject : inputSubjects) {
        dis.add(subject.getDiscipline());
    }
    return dis;
}

I looped through the subjects passed in and added the discipline of each to a new array list and returned it. I also changed List<?> to List<Subject> because we are expecting the client code to pass a list of subjects.

Another shorter way to implement this:

public static List<String> allDisciplines(ArrayList<Subject> inputSubjects){
    return inputSubjects.stream().map(Subject::getDiscipline).collect(Collectors.toList());
}

Now you can call allDisciplines by

Subject.allDisplines(subjects)

in Testing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download