【问题标题】:Referncing specific variable in ArrayList object引用 ArrayList 对象中的特定变量
【发布时间】:2017-04-07 15:58:43
【问题描述】:

我的问题涉及在 ArrayList 的对象中搜索特定变量并返回该变量。我的问题之一是 Student 对象包含多个变量字段。

我正在尝试在对象中定位特定变量,然后从数组列表中返回它。

这是主要的方法:

    Student s1 = new Student ("student1", 123, "MIS");
    Student s2 = new Student("student2", 231, "Finance");
    Student s3 = new Student ("student3", 432, "MIS");
    Student s4 = new Student ("student4", 438, "Marketing");
    Student s5 = new Student ("student5", 429, "MIS");
    Student s6 = new Student ("student6", 215, "Accounting");
    Student s7 = new Student ("student7", 287, "MIS");
    Student s8 = new Student ("student8", 401, "MIS");

    ArrayList<Student> myList = new ArrayList<Student>();
    myList.add(s1);
    myList.add(s2);
    myList.add(s3);
    myList.add(s4);
    myList.add(s5);
    myList.add(s6);
    myList.add(s7);
    myList.add(s8);

    StudentDatabaseImplementation st = new StudentDatabaseImplementation(); 
    st.setTheListOfStudents(myList);
    st.printTheListOfStudents(myList);


    st.getStudent(429);
    System.out.println("The name of the student : "+    
    st.getStudent(429).getName());

在我创建的 Student 类中我写道:

   public class Student{
   private String name;
   private int CWID, ID;
   private String major, maj;

   private ArrayList<Student> myList = new ArrayList<Student>()

   public Student(String name, int CWID, String major)
   {this.name=name;CWID=ID; String Major = maj;}

   public String getName() {return name;}

   public void setName(String name) {this.name = name;}

   public ArrayList<Student> getAllStudents() {return myList;}

   -->public Student getStudent(int CWID){if(myList.contains CWID)   
     {getName();}}

   public void setTheListOfStudents(ArrayList<Student> myList) {
   this.myList = myList;}

   public String getMajorName() {return majorName;}

   public void setMajorName(String majorName) {this.majorName = majorName;}

   public int getCWID() {return CWID;}

   public void setCWID(int studentId) {this.studentCWID = studentCWID;}

   private String majorName;
   private int studentCWID;

我还能做些什么来返回用户名吗?

【问题讨论】:

  • 您的代码看起来不错。使用当前的 getter 获取学生姓名有什么问题?
  • 你的StudentDatabaseImplementation怎么样?
  • @TimBiegeleisen 我希望我可以,但是对于这个作业,getStudent 方法是必需的。但是,myList.contains CWID 不会运行,因为它说找不到变量 CWID。
  • 这里出现错误:if(myList.contains CWID) --&gt; if(myList.contains(CWID))。您还应该实现 equals() 方法。
  • @MuratK。我必须使用一个接口来实现 StudentDatabaseImplementation 类,所以我也只需将主代码复制并粘贴到那里。有错吗?

标签: java arrays object methods interface


【解决方案1】:

这就是你应该如何实现getStudent 方法。如果未找到学生,则返回 null,或者如果列表中至少有具有该 CWID 的学生,则具有 CWID 的学生等于 CWID 参数。

public Student getStudent(int CWID){
    return myList.stream().filter(s -> s.getCWID()==CWID).findAny().orElse(null);
}

我建议您评估地图以存储学生。

Map<int,Student>

其中的键是 CWID。这样,您不应该重复查找学生。

【讨论】:

  • Student foundStudent= new Student("",0,""); ` for(int i=0; i
  • 我会使用 myList.contains(CWID) 方法,但它只有在我在答案中建议的 Student 类中实现 equals 方法时才有效。
  • 您在上面放置的方法...我只是想知道您将如何在公共 Student getStudent(int CWID) 上下文中使用它。每当我尝试调用布尔方法时,它都不会返回 Student 变量。
【解决方案2】:

虽然您的代码看起来不错,但您可以覆盖Student 类的toString() 方法并使其返回用户名。

public class Student {

    @Override
    public String toString() {
        return getName();
    }

}

那你就可以打电话了

System.out.println("The name of the student : "+    
st.getStudent(429));

【讨论】:

  • 我必须使用getStudent方法。
  • 它在那里:st.getStudent(429)
  • 公共 Student getStudent(int CWID) 方法仍然需要返回一个 Student 值。
猜你喜欢
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 2012-11-23
  • 2018-04-13
  • 1970-01-01
  • 2016-08-27
相关资源
最近更新 更多