简体   繁体   中英

Java different objects in a single method

I Know there are a bunch of questions related with this topic. Most of the answers states "use a interface" or "create a generic". Tried both and didnt work =( dont know what I'm doing wrong.

Here is the problem: I have two classes: Subjects and Courses. In the main section, I want a function that will receive an id (provided by the user) and look into, for example, a subjectList, trying to find if the subject is there, if yes, return its index. The logic is the same for both Courses and Subjects, so I'm trying to let this function become a bit more generic.

 public static void main(String[] args) {
        
             //Objects created along the code are stored here
             ArrayList<Course> courseList = new ArrayList<>();   
             ArrayList<Subject> subjectList = new ArrayList<>();
        
            public static Integer findObjectById(int id, ArrayList<IStudyDetails> object_list) {
                for (int i = 0; i < object_list.size(); i++) {
                    if (id == object_list.get(i).getId()) {
                        return i;
                    }
                }
                return null;
            }
    int index_subject = findObjectById(subject_id,subjectList);
    }

Here is the Interface: I tried to create this after look into some Stack Overflow related topics.

public interface IStudyDetails{
    int getId();

}

Here is the Course class: (I did hide most of the constructors/gets and setters)

public class Course implements IStudyDetails {
    private int id;
    private String name;
    public int getId() {return id;}}

Here is the Subject class (I did hide most of the constructors/gets and setters)

public class Subject implements IStudyDetails {
    private int id;
    private String name;
    public int getId() {return id;}}

THe error I'm receiving is:

java: incompatible types: java.util.ArrayList<entities.Subject> cannot be converted to java.util.ArrayList<entities.IStudyDetails>

public static void main(String[] args) {
    //Objects created along the code are stored here
    List<IStudyDetails> courseList = new ArrayList<>();
    List<IStudyDetails> subjectList = new ArrayList<>();
    Subject subject = new Subject(0001, "John");
    Subject subject1 = new Subject(1001, "Tom");
    subjectList.add(subject);
    subjectList.add(subject1);
    int subject_id = 1001;
    int index_subject = findObjectById(subject_id,subjectList);
}
public static Integer findObjectById(int id, List<IStudyDetails> object_list) {
    for (int i = 0; i < object_list.size(); i++) {
        if (id == object_list.get(i).getId()) {
            System.out.println(i);
            return i;
        }
    }
    return null;
}

I Just change the List Type, Cause both Course and Subject are implemented from IStudyDetails, so you need to use IStudyDetails as a type to create the list and it can work.

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