在Java中从对象数组中删除对象
我目前正在一个学校项目中学习使用对象数组。在此过程中,我遇到了两个不同的问题,我想谈谈。我将NetBeans IDE与下面提到的所有代码一起使用 首先,在向数组添加值之后,我尝试使用一个列表按钮,该按钮应在jtextArea中显示值。虽然所有的值都显示为“null”,而不是用户输入的值,但不会弹出明显的错误。由于有5个值,程序显示“null”。我不知道这是为什么,任何意见将不胜感激 其次,我试图允许用户根据存储的5个值之一删除信息。对于本程序,用户必须输入“员工ID”以从程序列表中删除所述员工的所有数据。我很难做到这一点,你可以看到我的代码如下 总而言之: 如何更正此随机“空”错误,以便显示输入的数据? 如何根据用户输入的idNumber有效地删除给定索引的值 要记住的事情: -我需要使用一个对象数组 -我对编码相当陌生,所以请原谅我可能的无知 -任何看起来缺少的东西都可能包含在NetBeans代码中,因此可能没有包含在内,这是我心目中主要关注的代码 代码如下:在Java中从对象数组中删除对象,java,arrays,Java,Arrays,我目前正在一个学校项目中学习使用对象数组。在此过程中,我遇到了两个不同的问题,我想谈谈。我将NetBeans IDE与下面提到的所有代码一起使用 首先,在向数组添加值之后,我尝试使用一个列表按钮,该按钮应在jtextArea中显示值。虽然所有的值都显示为“null”,而不是用户输入的值,但不会弹出明显的错误。由于有5个值,程序显示“null”。我不知道这是为什么,任何意见将不胜感激 其次,我试图允许用户根据存储的5个值之一删除信息。对于本程序,用户必须输入“员工ID”以从程序列表中删除所述员工的
//ArrayList of Objects Declared
ArrayList <employees> list = new ArrayList <employees>();
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
employees e;
//Declaring Strings
String idNumber, firstName, lastName, annualSalary, startDate;
//Reads the input fields
idNumber = idInput.getText();
firstName = fNameInput.getText();
lastName = lNameInput.getText();
annualSalary = annualInput.getText();
startDate = startDateInput.getText();
//Sends input information as a class
e = new employees(idNumber, firstName, lastName, annualSalary, startDate);
//If data is missing, the user will receive and error message
if (idNumber.isEmpty()||firstName.isEmpty()||lastName.isEmpty()||annualSalary.isEmpty()||startDate.isEmpty()){
infoOutput.setText("Please fill out all catagories!");
}
//Otherwise, the information provided will be stored
else {
list.add(e);
infoOutput.setText("");
infoOutput.setText("Employee data added!");
}
}
private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Reset infoOutput
infoOutput.setText("");
employees e;
//Declaring Strings
String idNumber, firstName, lastName, annualSalary, startDate;
//Reads the input fields
idNumber = idInput.getText();
firstName = fNameInput.getText();
lastName = lNameInput.getText();
annualSalary = annualInput.getText();
startDate = startDateInput.getText();
//Sends input information as a class
e = new employees(idNumber, firstName, lastName, annualSalary, startDate);
//Determines if the requested employee exists in the list
for (int index = 0; index < list.size(); index++) {
//If the employee ID is found, their information is removed
if (list.get(index).toString() == idNumber){
infoOutput.setText("Employee data removed!");
list.remove(index);
}
//Otherwise, an error message is displayed to the user
else{
infoOutput.setText("The employee ID " + idNumber + " was not found!");
}
}
}
class employees {
String idNumber, firstName, lastName, annualSalary, startDate;
employees(String idNumber, String firstName, String lastName, String annualSalary, String startDate) {
idNumber = idInput.getText();
firstName = fNameInput.getText();
lastName = lNameInput.getText();
annualSalary = annualInput.getText();
startDate = startDateInput.getText();
}
}
private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Reset temp
String temp = "";
//List all of the stored data
for (int x = 0; x <= list.size()-1; x++) {
temp = temp + list.get(x).idNumber + " "
+ list.get(x).firstName + " "
+ list.get(x).lastName + " "
+ list.get(x).annualSalary + " "
+ list.get(x).startDate + "\n";
}
outputField.setText(temp);
}
//声明的对象的ArrayList
ArrayList=新的ArrayList();
私有void addButtonActionPerformed(java.awt.event.ActionEvent evt){
雇员e;
//声明字符串
字符串idNumber、firstName、lastName、annualSalary、startDate;
//读取输入字段
idNumber=idInput.getText();
firstName=fNameInput.getText();
lastName=lNameInput.getText();
annualSalary=annualInput.getText();
startDate=startDateInput.getText();
//将输入信息作为类发送
e=新员工(ID号、姓氏、姓氏、年历、起始日期);
//如果数据丢失,用户将收到错误消息
如果(idNumber.isEmpty()| | | | | | | | | | | | lastName.isEmpty()| | annualSalary.isEmpty()| | startDate.isEmpty()){
setText(“请填写所有类别!”);
}
//否则,将存储提供的信息
否则{
列表.添加(e);
infoOutput.setText(“”);
infoOutput.setText(“添加了员工数据!”);
}
}
private void removeButtonActionPerformed(java.awt.event.ActionEvent evt){
//重置信息输出
infoOutput.setText(“”);
雇员e;
//声明字符串
字符串idNumber、firstName、lastName、annualSalary、startDate;
//读取输入字段
idNumber=idInput.getText();
firstName=fNameInput.getText();
lastName=lNameInput.getText();
annualSalary=annualInput.getText();
startDate=startDateInput.getText();
//将输入信息作为类发送
e=新员工(ID号、姓氏、姓氏、年历、起始日期);
//确定列表中是否存在请求的员工
对于(int index=0;index 对于(int x=0;x,employees的构造函数不初始化实例变量,而是初始化参数
this.idNumber = idNumber;
对其余的实例变量执行类似的语句。这就是employee类中所有值都为null的原因
测试idNumber时,您正在将员工的toString()与idNumber进行比较。必须将列表中员工的idNumber与UI中的idNumber进行比较
if (list.get(index).idNumber.equals(idNumber)) {
最好为employee类中的每个属性创建一个访问器。下面是带有getData
方法的employees
类,如我在注释中所述。(我还更改了构造函数,由@downyt更正)
使用此方法,打印代码将更改为类似以下内容:
for (int x = 0; x < list.size(); x++){
temp = list.get(x).getData() + "\n";
}
outputField.setText(temp);
数组或ArrayList
?数组的大小不可调整,因此需要创建另一个小一个元素的数组,并复制要保留的值。如果它是ArrayList
,则可以调用。If(list.get(index.toString()==idNumber)
这将始终计算为false,因为您将从对象
中提取的字符串
与用户在ID字段中输入的特定字符串进行比较。相反,您需要根据idNumber检查ID文本<
for (int x = 0; x < list.size(); x++){
temp = list.get(x).getData() + "\n";
}
outputField.setText(temp);
temp += list.get(x).getData();