我想删除用户输入详细信息的特定员工。我在java.Deletion操作中使用序列化操作不按我想要的方式执行。我想删除用户输入详细信息的员工。但是它会删除所有员工
import java.io.*;
import java.util.*;
public class SerializationDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException{
//choice provided to the user
System.out.println("Welcome To Employee Management System");
System.out.println("Enter your choice");
System.out.println("Enter 1:->show employees");
System.out.println("Enter 2:->Create new employee");
System.out.println("Enter 3:->Delete employee");
System.out.println("Enter 4:->quit");
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
ArrayList<Myclass> object1 = new ArrayList<Myclass>();
//insert operation
if(choice==2){
try{
String str1;
String str2;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number of employees you want to add");
Scanner in1 = new Scanner(System.in);
int num = in1.nextInt();
//ArrayList<Myclass> object1 = new ArrayList<Myclass>();
for(int i=0;i<num;i++){
str1 = br.readLine();
str2 = br.readLine();
object1.add(new Myclass(str1,str2));
}
FileOutputStream fos = new FileOutputStream("Myfile.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
}
catch(Exception e){
System.out.println("exception");
e.printStackTrace();
System.exit(0);
}
}
//show operation
if(choice==1){
try{
FileInputStream fis = new FileInputStream("Myfile.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList object2 = (ArrayList)ois.readObject();
ois.close();
System.out.println("employee details "+" "+object2);
}
catch(Exception e){
e.printStackTrace();
}
}
if(choice==3){
System.out.println("Enter name and department of employee you want to delete");
try {
String str3;
String str4;
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
str3 = br1.readLine();
str4 = br1.readLine();
/*FileInputStream Fs = new FileInputStream("Myfile");
ObjectInputStream Os = new ObjectInputStream(Fs);
ArrayList<Myclass> object3 = (ArrayList<Myclass>)Os.readObject();*/
object1.remove(new Myclass(str3,str4));
FileOutputStream fs = new FileOutputStream("Myfile.txt");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(object1);
os.flush();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//exit operation
if(choice==4){
System.exit(0);
}
}
}
class Myclass implements Serializable{
String name;
String dname;
public Myclass(String name,String dname){
this.name = name;
this.dname = dname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return dname;
}
public void setDepartment(String dname) {
this.dname = dname;
}
public String toString(){
return "name = "+" "+name+","+" "+"department = "+" "+dname;
}
}
答案 0 :(得分:1)
我在这里看到两个问题。
object1
列表。equals()
和hashCode()
方法。 list.remove()不会因为它无法从列表中找到你给出的对象。答案 1 :(得分:0)
@AmitChotaliya已经清楚地解释了
这是从ArrayList
中删除对象的示例首先你需要反序列化
FileInputStream Fs = new FileInputStream("Myfile.txt");
ObjectInputStream Os = new ObjectInputStream(Fs);
ArrayList<Myclass> object3 = (ArrayList<Myclass>)Os.readObject();
然后删除它,但这是快速失败的
for(int index = 0; i < object3.size(); i++)
{
if(object3.get(i).getName().equals(str3) && object3.get(i).getDepartment().equals(str4))
{
object3.remove(index);
break;
}
}
但是如果你想用失败保险箱删除它
Iterator itr = object3.iterator();
while(itr.hasNext()) {
Myclass element = (Myclass) itr.next();
if(element.getName().equals(str3) && element.getDepartment().equals(str4))
{
itr.remove();
break;
}
}