如何从java中的链表中删除一个对象? [英] how to remove a object from linked list in java?

查看:286
本文介绍了如何从java中的链表中删除一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有一个问题,我做了一个示例程序来显示从一个链表的emp细节,现在的问题,当我试图删除一个特定的条目意味着它不工作,我希望我做了一些错误在我的代码中,你可以建议怎么做?

  import java.util。*; 

class EmpDedup {
int record;
String fprint;
int fid;

EmpDedup(int record,String fprint,int fid){
this.record = record;
this.fprint = fprint;
this.fid = fid;
}

public int getRecord(){
return record;
}

public String getFprint(){
return fprint;
}

public int getFid(){
return fid;
}

public static void main(String [] args){
int count = 0;
LinkedList&EmpDedup> list = new LinkedList&EmpDedup>();
list.add(new EmpDedup(101,entry1,20));
list.add(new EmpDedup(102,entry2,30));
list.add(new EmpDedup(103,entry3,40));
list.add(new EmpDedup(104,entry4,50));

扫描仪输入=新的扫描仪(System.in);
System.out.print(输入记录不显示);
int rec = input.nextInt();
for(EmpDedup data:list){
if(data.getRecord()== rec){
System.out.println(data.getRecord()+\t+ data.getFprint()+\t+ data.getFid()+\t);

count ++;

}
}
System.out.println(链接的大小是:\t+ list.size());

System.out.println(可用记录数为:+ count);

System.out.println(链接的大小是:\t+ list.size());
扫描仪input1 =新的扫描仪(System.in);
System.out.print(输入记录no to delete:); //这里我尝试删除一个特定的记录
int rec1 = input1.nextInt();
for(EmpDedup data:list){
if(data.getRecord()== rec1){
// System.out.println(data.getRecord()+\t + data.getFprint()+\t+ data.getFid()+\t);
list.remove(data); //问题在这里
count ++;

}
}
}
}


解决方案

您不能在列表中添加(添加,删除...项目)。您必须使用Iterator

 (Iterator& EmpDedup> iter = list.iterator(); iter.hasNext(); ){
EmpDedup data = iter.next();
if(data.getRecord()== rec1){
iter.remove();
}
}

请参阅 http://docs.oracle.com/javase/6/docs/api/java/util/Iterator .html


i have one problem with my code ,i did a sample program to display the emp details from a linked list,now the problem when i trying to delete a particular entry means it doesn't work,i hope i did some mistake in my code could you suggest how to do that?

import java.util.*;

class EmpDedup {
    int record;
    String fprint;
    int fid;

    EmpDedup(int record, String fprint, int fid) {
        this.record = record;
        this.fprint = fprint;
        this.fid = fid;
    }

    public int getRecord() {
        return record;
    }

    public String getFprint() {
        return fprint;
    }

    public int getFid() {
        return fid;
    }

    public static void main(String[] args) {
        int count = 0;
        LinkedList<EmpDedup> list = new LinkedList<EmpDedup>();
        list.add(new EmpDedup(101, "entry1", 20));
        list.add(new EmpDedup(102, "entry2", 30));
        list.add(new EmpDedup(103, "entry3", 40));
        list.add(new EmpDedup(104, "entry4", 50));

        Scanner input = new Scanner(System.in);
        System.out.print("Enter record no to display: ");
        int rec = input.nextInt();
        for (EmpDedup data : list) {
            if (data.getRecord() == rec) {
                System.out.println(data.getRecord() + "\t" + data.getFprint() + "\t" + data.getFid() + "\t");

                count++;

            }
        }
        System.out.println("The size of an linkedlist is: \t" + list.size());

        System.out.println("The number of  available record  is :" + count);

        System.out.println("The size of an linkedlist is: \t" + list.size());
        Scanner input1 = new Scanner(System.in);
        System.out.print("Enter record no to delete: ");// here i try to delete a particular record
        int rec1 = input1.nextInt();
        for (EmpDedup data : list) {
            if (data.getRecord() == rec1) {
                // System.out.println(data.getRecord()+"\t"+data.getFprint()+"\t"+data.getFid()+"\t");
                list.remove(data); // problem is here
                count++;

            }
        }
    }
}

解决方案

you cannot operate in lists (add, remove... items) while you iterate on them. You have to use an Iterator

for(Iterator<EmpDedup> iter = list.iterator(); iter.hasNext();) {
    EmpDedup data = iter.next();
    if (data.getRecord() == rec1) {
        iter.remove();
    }
}

see http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html

这篇关于如何从java中的链表中删除一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆