2012-05-24 60 views
3

我有一個問題,我的代碼,我做了一個示例程序來顯示鏈接列表中的emp詳細信息,現在問題,當我試圖刪除一個特定的條目意味着它不工作,我希望我做了一些錯誤我的代碼可以建議如何做到這一點?如何從java中的鏈表中刪除一個對象?

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

      } 
     } 
    } 
} 
+2

「無法工作」 的頭 - 請更多的描述。發生了什麼? –

+4

list.remove(101);如果這不是list.remove(data); –

回答

10

在迭代它們時,您無法在列表中操作(添加,刪除...項目)。你必須使用一個Iterator

for(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

+0

感謝它的工作。 –

7

使用一個Iterator代替,然後使用上Iterator

5

remove()方法。當你這樣做:

list.remove(101);

您所呼叫this方法,它不會成爲你的目的。

你還是使用:

list.remove(data);

2

你嘗試用指數101刪除元素,但你必須在列表中只有4項。如何刪除索引爲101的元素?

如果要刪除等於EmpDedup(101,「entry1」,20)的元素,則必須傳入remove方法中的此對象。並且不要重新定義EmpDedup的equals和hash代碼。

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

    } 
} 
2

代碼試圖索引位置101移除元素,但只有四個列表中的項目。

使用下列作爲替代你的代碼:

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

這就是一個列表對象將被刪除。

0

import java.util.Scanner;

//一個完整的工作Java程序證明單 //鏈表 LinkedList類 { 節點頭缺失; //列表

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

}

相關問題