How to remove an object from a linked list in java?

I have one problem with my code, I made an example program for displaying emp details from a linked list, now the problem when I try to delete a specific record means that it does not work, I hope I made some mistake in my code could you suggest how to do this?

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++; } } } } 
+3
source share
7 answers

you cannot work in lists (add, delete ... items) while you repeat them. You must use 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

+10
source

Use Iterator instead, and then use the remove() method on Iterator

+7
source

When you do this:

list.remove(101);

you call this method that will not serve your purpose.

You should use:

list.remove(data);

+5
source

You are trying to delete an item with index 101, but you only have 4 items in the list. How can he delete an item with index 101?

If you want to delete an element equal to EmpDedup (101, "entry1", 20), you need to pass a method to delete this object. And no need to enter overridden values ​​and the hash code of EmpDedup.

+2
source

The code tries to remove an element at position 101 of the index, but there are only four elements in the list.

Use the following to replace the code:

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

That the list object will be deleted.

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

import java.util.Scanner;

// Full working Java program to demonstrate deletion alone // linked list LinkedList class {Node head; // list head

 /* 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(); } 

}

0
source

Source: https://habr.com/ru/post/1498937/


All Articles