Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中从链表中删除对象?_Java_Data Structures_Linked List_Singly Linked List - Fatal编程技术网

如何在java中从链表中删除对象?

如何在java中从链表中删除对象?,java,data-structures,linked-list,singly-linked-list,Java,Data Structures,Linked List,Singly Linked List,我的代码有一个问题,我做了一个示例程序来显示链接列表中的emp详细信息,现在当我试图删除一个特定条目时出现问题意味着它不起作用,我希望我的代码中出现了一些错误。你能建议如何做吗 import java.util.*; class EmpDedup { int record; String fprint; int fid; EmpDedup(int record, String fprint, int fid) { this.record = r

我的代码有一个问题,我做了一个示例程序来显示链接列表中的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));

        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++;

            }
        }
    }
}
import java.util.*;
类重复数据消除{
int记录;
字符串打印;
int-fid;
EMPDEAD(整数记录、字符串fprint、整数fid){
this.record=记录;
this.fprint=fprint;
this.fid=fid;
}
public int getRecord(){
返回记录;
}
公共字符串getFprint(){
返回fprint;
}
公共int getFid(){
返回fid;
}
公共静态void main(字符串[]args){
整数计数=0;
LinkedList=新建LinkedList();
添加(新的重复数据消除(101,“entry1”,20));
添加(新的重复数据消除(102,“entry2”,30));
添加(新的重复数据消除(103,“entry3”,40));
添加(新的重复数据消除(104,“entry4”,50));
扫描仪输入=新扫描仪(System.in);
系统输出打印(“输入要显示的记录编号:”);
int rec=input.nextInt();
用于(重复数据消除:列表){
if(data.getRecord()==rec){
System.out.println(data.getRecord()+“\t”+data.getFprint()+“\t”+data.getFid()+“\t”);
计数++;
}
}
System.out.println(“linkedlist的大小为:\t”+list.size());
System.out.println(“可用记录数为:“+count”);
System.out.println(“linkedlist的大小为:\t”+list.size());
扫描仪输入1=新扫描仪(System.in);
System.out.print(“输入要删除的记录编号:”;//这里我尝试删除一条特定的记录
int rec1=input1.nextInt();
用于(重复数据消除:列表){
if(data.getRecord()==rec1){
//System.out.println(data.getRecord()+“\t”+data.getFprint()+“\t”+data.getFid()+“\t”);
list.remove(data);//问题在这里
计数++;
}
}
}
}

执行此操作时,请改用
迭代器,然后使用
迭代器上的
remove()
方法:

list.remove(101)

您正在调用方法,这将不符合您的目的

你应该使用:


列表。删除(数据)

您尝试删除索引为101的元素,但列表中只有4项。如何删除索引为101的元素


如果要删除等于EMPDEAD(101,“entry1”,20)的元素,则必须传入remove方法来删除此对象。不要忘记重新定义EMPDead的equals和hash代码。

在列表上迭代时,不能在列表中操作(添加、删除…项)。您必须使用迭代器

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

请参见

代码试图删除索引位置101处的元素,但列表中只有四项

使用以下代码替换您的代码:

for( EmpDedup data : list)
{
   if( data.getRecord() == rec1 )
   {
        list.remove( data );     
         ++count;
    }
}

这就是列表对象将被删除的位置。

import java.util.Scanner

//一个完整的Java程序,用于单独演示删除操作 //链表 类链接列表 { 节点头;//列表头

/* Linked list Node*/
class Node
{
    int data;
    Node next;
    Node(int d)
    {
        data = d;
        next = null;
    }
}

/* Given a key, deletes the first occurrence of key in linked list */
void deleteNode(int key)
{
    // Store head node
    Node temp = head, prev = null;

    // If head node itself holds the key to be deleted
    if (temp != null && temp.data == key)
    {
        head = temp.next; // Changed head
        return;
    }

    // Search for the key to be deleted, keep track of the
    // previous node as we need to change temp.next
    while (temp != null && temp.data != key)
    {
        prev = temp;
        temp = temp.next;
    }    

    // If key was not present in linked list
    if (temp == null) return;

    // Unlink the node from linked list
    prev.next = temp.next;
}

/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
    Node new_node = new Node(new_data);
    new_node.next = head;
    head = new_node;
}

/* This function prints contents of linked list starting from
    the given node */
public void printList()
{
    Node tnode = head;
    while (tnode != null)
    {
        System.out.print(tnode.data+" ");
        tnode = tnode.next;
    }
}

/* Drier program to test above functions. Ideally this function
should be in a separate user class. It is kept here to keep
code compact */
public static void main(String[] args)
{
    LinkedList llist = new LinkedList();

    llist.push(7);
    llist.push(1);
    llist.push(3);
    llist.push(2);

    System.out.println("\nCreated Linked list is:");
    llist.printList();

    Scanner sc = new Scanner(System.in);
    System.out.println("please enter input");
    int aa =sc.nextInt();
    llist.deleteNode(aa); // Delete node at position 4

    System.out.println("\nLinked List after Deletion at position :"+aa);


    llist.printList();

    System.out.println("adding element to the LinkedList");

    System.out.println("enter element");
    int dd = sc.nextInt();
    llist.push(dd);

    llist.printList();
}
}

“无法工作”-请更详细地描述。发生了什么?列表。删除(101);如果不是列表,请删除(数据);
/* Linked list Node*/
class Node
{
    int data;
    Node next;
    Node(int d)
    {
        data = d;
        next = null;
    }
}

/* Given a key, deletes the first occurrence of key in linked list */
void deleteNode(int key)
{
    // Store head node
    Node temp = head, prev = null;

    // If head node itself holds the key to be deleted
    if (temp != null && temp.data == key)
    {
        head = temp.next; // Changed head
        return;
    }

    // Search for the key to be deleted, keep track of the
    // previous node as we need to change temp.next
    while (temp != null && temp.data != key)
    {
        prev = temp;
        temp = temp.next;
    }    

    // If key was not present in linked list
    if (temp == null) return;

    // Unlink the node from linked list
    prev.next = temp.next;
}

/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
    Node new_node = new Node(new_data);
    new_node.next = head;
    head = new_node;
}

/* This function prints contents of linked list starting from
    the given node */
public void printList()
{
    Node tnode = head;
    while (tnode != null)
    {
        System.out.print(tnode.data+" ");
        tnode = tnode.next;
    }
}

/* Drier program to test above functions. Ideally this function
should be in a separate user class. It is kept here to keep
code compact */
public static void main(String[] args)
{
    LinkedList llist = new LinkedList();

    llist.push(7);
    llist.push(1);
    llist.push(3);
    llist.push(2);

    System.out.println("\nCreated Linked list is:");
    llist.printList();

    Scanner sc = new Scanner(System.in);
    System.out.println("please enter input");
    int aa =sc.nextInt();
    llist.deleteNode(aa); // Delete node at position 4

    System.out.println("\nLinked List after Deletion at position :"+aa);


    llist.printList();

    System.out.println("adding element to the LinkedList");

    System.out.println("enter element");
    int dd = sc.nextInt();
    llist.push(dd);

    llist.printList();
}