Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用actionListener更新和删除arrayList中的对象_Java_User Interface_Arraylist_Actionlistener - Fatal编程技术网

Java 使用actionListener更新和删除arrayList中的对象

Java 使用actionListener更新和删除arrayList中的对象,java,user-interface,arraylist,actionlistener,Java,User Interface,Arraylist,Actionlistener,我正在创建一个类似于学生注册系统的程序。我可以添加任意数量的学生,并且可以通过搜索他们的ID号来查找学生。但是我被删除和更新学生的代码卡住了。有什么建议吗 import java.io.Serializable; import java.util.ArrayList; public class School implements Serializable { ArrayList<Student>students; /** Creates an arrayLi

我正在创建一个类似于学生注册系统的程序。我可以添加任意数量的学生,并且可以通过搜索他们的ID号来查找学生。但是我被删除和更新学生的代码卡住了。有什么建议吗

import java.io.Serializable;
import java.util.ArrayList;

public class School implements Serializable
{
    ArrayList<Student>students;
    /**
    Creates an arrayList that holds student objects
    */
    public School()
    {
        students = new ArrayList<Student>();
    }
    /**
    Adds a student to the arrayList
    */
    public void addStudent(Student a)
    {
        students.add(a);
    }
    public String toString()
    {
        String content = "";

        if(!students.isEmpty())
        {
            for(int i = 0; i<students.size(); i++)
            {

                content = content + students.get(i).getID() + " " + students.get(i).getName() + " " + students.get(i).getMajor() + " " + students.get(i).getQPA() + "\n"; 
            } 
        }
        return content;
    }
    public void remove(Student a)
    {
        remove(a);
    }
}
您应该从
学生中删除。您只需删除(a)

你可以这样做让学生

 public void actionPerformed (ActionEvent event)
 {
       if(!firstSchoolOfJava.equals(idField.getText()))
       {
           // input should first be parsed to an int (id type is an int)
           int id = Integer.parseInt(idField.getText());
           Student s;
           for (int i = 0; i < firstChoolOfJava.getStudents().size(); i++){
               // check to see if the ids match. If they do, remove that student
               if ((firstChoolOfJava.getStudents().get(i).getID()) == id) {
                   s = firstChoolOfJava.getStudents().get(i);
                   firstSchoolOfJava.remove(s);  // you're removing this student
                   break;
               }                   
           }
       }
       System.out.println(firstSchoolOfJava.toString());
 }
更新学生

public void actionPerformed (ActionEvent event)
{
    String name = nameField.getText();
    String major = majorField.getText();
    double gpa = Double.parseDouble(gpaField.gtText());
    int id = Integer.parseInt(idField.getText());

    Student s = new Student(id, name, major, gpa); // or whatever your constructor looks like.
    Student s1;
    for (int i = 0; i < firstSchoolOfJava.getStudents().size(); i++){
        s1 = firstSchoolOfJava.getStudents().get(i);
        if (s1.getId() == id){
            firstSchoolOfJava.getStudents().set(i, s);
            break;
        }
    }   
} 
public void actionPerformed(ActionEvent事件)
{
String name=nameField.getText();
字符串major=majorField.getText();
double-gpa=double.parseDouble(gpaField.gtText());
int id=Integer.parseInt(idField.getText());
Student s=新学生(id、姓名、专业、gpa);//或任何构造函数的外观。
学生s1;
for(inti=0;i

注:以上代码考虑的是所有字段都已正确填写。如果您只想更新一个其他字段为空的字段,则需要进行一些检查以查看哪些字段为空。

您的问题需要更清楚。是否要删除所有学生?或者,是否要根据唯一的学生ID删除学生?为了让您开始,下面是一些基本的伪代码:

public boolean removeStudent(int uniqueID)
{
   for(loop thru students)
   { 
     if(listOfStudents.get(i).getId() == uniqueID)
     {
       listOfStudents.remove(i);
       updateView();
       return true;
     }
   }

 return false;
}
此外,当您添加学生时,请执行以下操作:

 /*
  * Adds a student to the arrayList
  */
  public void addStudent(Student a)
  {
    students.add(a);  <-- You call add() from the ArrayList of students.
  }
public void remove(Student a)
{
    remove(a);  <-- You do not call the ArrayList's remove() method at all. Instead,
                    you call the local remove() method defined in your School class.
}
 public void remove(Student a)
 {
     students.remove(a);  <-- Call the ArrayList's remove() method instead. 
 }

这将导致一个无限循环。那么actionListener3class按钮Listener3中firstSchoolOfJava.remove()的参数是什么呢?Listener3实现ActionListener{public void actionPerformed(ActionEvent事件){if(!firstSchoolOfJava.equals(idField.getText()){firstSchoolOfJava.remove();}System.out.println(firstSchoolOfJava.toString());}ButtonListener3 listener3=新建ButtonListener3();remove.addActionListener(listener3);看看我的答案。我想这就是你要找的。您通过检查Id来获取学生。如果Id匹配,则将该学生作为参数传递。如果(!firstSchoolOfJava.equals(idField.getText())
,我不确定我是否真正理解这段代码。你似乎在检查学校是否与id相等。这是你的意图吗?id字段不是应该获得学生id吗?一个建议是:学生的studID应该是唯一的吗?如果是这样,您可以使用映射而不是ArrayList来简化操作。在
ActionListener1
中应该发生什么?你没有代码吗?ActionListener1是更新学生信息的地方。我甚至不知道从哪里开始写代码。好的,在这种情况下,你需要为学生专业和gpa创建访问器和变异器。因此,在学生课堂上创建一个setMajor(String-major)和一个setGPA(double-gpa)。
 /*
  * Adds a student to the arrayList
  */
  public void addStudent(Student a)
  {
    students.add(a);  <-- You call add() from the ArrayList of students.
  }
public void remove(Student a)
{
    remove(a);  <-- You do not call the ArrayList's remove() method at all. Instead,
                    you call the local remove() method defined in your School class.
}
 public void remove(Student a)
 {
     students.remove(a);  <-- Call the ArrayList's remove() method instead. 
 }
public void updateStudent(Student student, double GPA, String major)
{
  for(loop thru list of students)
  {
     if(listOfStudents.get(i).equals(student))
     {
       listOfStudents.get(i).setGPA(GPA);
       listOfStudents.get(i).setMajor(major);
     }
  }
}