以员工记录程序为例,它将员工姓名、ID#、年薪和雇用日期存储在一个 ArrayList 中。
ArrayList <Company> inventory = new ArrayList <Company>();
String ID, firstName, lastName, annualSal, startDate;
这存储了信息。
private void AddActionPerformed(java.awt.event.ActionEvent evt) {
ID = IDField.getText();
firstName = firstNameField.getText();
lastName = lastNameField.getText();
annualSal = annualSalField.getText();
startDate = startDateField.getText();
Company c = new Company(ID, firstName, lastName, annualSal, startDate);
inventory.add(c);
}
这会在调用时输出列表中的信息。
private void ListActionPerformed(java.awt.event.ActionEvent evt) {
String temp = "";
for (int x=0; x<=inventory.size()-1; x++) {
temp = temp + inventory.get(x).ID + " "
+ inventory.get(x).firstName + " "
+ inventory.get(x).lastName + " "
+ inventory.get(x).annualSal + " "
+ inventory.get(x).startDate + "\n";
}
employeeTArea.setText(temp);
此按钮将删除选定的信息(通过输入 ID#)并删除员工的记录。
private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Code here on how to delete an unknown variable stored in array, possible to use getText?
}