我创建了一个程序,它与包含三个字符串的ArrayList
一起使用 - 名称,ID和国家/地区。程序中的一个选项是在TextField
内写下姓名,国家或ID,然后按一个按钮Delete
删除包含输入数据的ArrayList
内的所有对象。在Delete类中调用此方法deleteObject
。调用方法containsField
方法内部,该方法应检查数据是否包含在给定对象中。当我运行程序时,我收到消息All items successfully deleted.
,但实际上没有删除任何对象。我尝试在程序中的不同位置打印出控制台上的不同数据,以便查明错误,但我无法找到它。你能帮帮我吗?
这是deleteObject:EDIT - 尝试使用litr.remove()删除对象:
public void deleteObject(String fileName, String userInput){
try {
ObjectInputStream os = new ObjectInputStream(new FileInputStream(fileName));
animal = (ArrayList<ANIMAL>) os.readObject();
System.out.println("Original contents of animal: ");
ListIterator litr = animal.listIterator();
//we call the boolean to check if user input is contained,
//need to get userInput from Delete bar
//we pass in a string, should return true
int k=0;
while(litr.hasNext()) {
Object anm = litr.next();
if( !(checkif.containsField(anm, userInput)) ){
litr.remove();
//if true we set k++, if k==0 in the end, then nothing was found;
}//if false, else will be executed, if true, would else be skipped?
else{
k++;
}
}
if (k==0){
JOptionPane.showMessageDialog(null, "This item does not exist.");
}
else {
JOptionPane.showMessageDialog(null, "All items successfully deleted.");
}
os.close();
}
catch (FileNotFoundException ex) {
System.out.println("Can't find the file "+fileName);
} catch (IOException ex) {
System.out.println("The file "+fileName+" has already been closed");
} catch (ClassNotFoundException ex) {
System.out.printf("Class is missing: %s\n",ex);
}
}
编辑 - 尝试通过将不包含用户输入的所有对象写入新文件来删除对象:
public void deleteObject(String fileName, String userInput){
try {
String fileNameTemp = "D:\\Workspace\\Kursova_2017\\src\\GUI_Try\\ZooTEMP.txt";
File ZooTEMP = new File(fileNameTemp);
ObjectInputStream os = new ObjectInputStream(new FileInputStream(fileName));
ObjectOutputStream osTMP = new ObjectOutputStream(new FileOutputStream(fileNameTemp));
animal = (ArrayList<ANIMAL>) os.readObject();
System.out.println("Original contents of animal: ");
ListIterator litr = animal.listIterator();
//we call the boolean to check if user input is contained,
//need to get userInput from Search/Delete bar
//we pass in a string, should return true
int k=0;
while(litr.hasNext()) {
Object anm = litr.next();
if( !(checkif.containsField(anm, userInput)) ){
osTMP.writeObject(animal);
//litr.remove();
//if true we set k++, if k==0 in the end, then nothing was found;
}//if false, else will be executed, if true, would else be skipped?
else{
k++;
}
}
if (k==0){
JOptionPane.showMessageDialog(null, "This item does not exist.");
}
else {
zooFile.delete();
ZooTEMP.renameTo(zooFile);
JOptionPane.showMessageDialog(null, "All items successfully deleted.");
}
os.close();
osTMP.close();
}
catch (FileNotFoundException ex) {
System.out.println("Can't find the file "+fileName);
} catch (IOException ex) {
System.out.println("The file "+fileName+" has already been closed");
} catch (ClassNotFoundException ex) {
System.out.printf("Class is missing: %s\n",ex);
}
}
这是检查对象是否包含输入的代码:
public static boolean containsField(Object anmObj, String Field) {
String lAnmObj = anmObj.toString().toLowerCase();
if(lAnmObj.contains(Field.toLowerCase())){
return true;
}
return false;
}