1

How could I remove an object inside a Linkedlist. I have a class account with studentId and studentName. I enter the objects inside the list, but when I try to remove I do not know how to do it. Because every time you remove an element from the middle of the list it gets organized, meaning the indexes change. So how can I get the studentId attribute and remove the object inside the linkedList.

Sample:

LinkedList: Account{studentId = 1, studentName = nome1} = index = 0 , 
LinkedList: Account{studentId = 2, studentName = nome2} = index = 1 , 
LinkedList: Account{studentId = 3, studentName = nome3} = index = 2.

what I would like was for the user to insert the studentId that he wants to delete and I can do a code that searches and deletes that object.

public Account{
    private int studentID;
    private String StudentName;
}

public static void main(String[] args){

    int accountNumber;

    LinkedList<Account> linkedAccount = new LinkedList<>();
    Account obj1;

    System.out.println("Type the acc number: ");
    accountNumber = in.nextInt();
    obj1 = linkedAccount.remove(accountNumber);
    System.out.println("The " + obj1 + " has been deleted");
}

Every time I delete an object from the middle it changes the index of the linkedList. Rearranging. So i do not know how to do it can you help me?

azro
  • 53,056
  • 7
  • 34
  • 70
DViga
  • 47
  • 6
  • What do you mean by **rearranging**? List cannot contain holes. If you have the following list: `index1 -> object1, index2 -> object2, index3 -> object3` and then you delete `object2` then your new list will look like this: `index1 -> object1, index2 -> object3` – Ivan Jan 08 '19 at 21:26

3 Answers3

2
  1. If you don't need to keep a reference to the object you remove, you can just

    linkedAccount.removeIf(acc -> acc.getStudentID() == accountNumber);
    
  2. If you want to keep a reference to the element you remove you can

    for (Account acc : linkedAccount) {
        if (acc.getStudentID() == accountNumber) {
            obj1 = acc;
            linkedAccount.remove(acc);
            break;
        }
    }
    
    // OR
    
    for (int i = 0; i < linkedAccount.size(); i++) {
        if (linkedAccount.get(i).getStudentID() == accountNumber) {
            obj1 = linkedAccount.remove(i);
            break;
        }
    }
    
  3. Notice that in most case and basiclly an ArrayList is sufficient When to use LinkedList over ArrayList in Java?

azro
  • 53,056
  • 7
  • 34
  • 70
  • Hi @azro. Thank you for reply me. What is acc in: linkedAccount.removeIf(acc -> acc.getStudentID() == accountNumber); and removeIf ? sorry I did not understand. Can you explain me? – DViga Jan 08 '19 at 22:19
  • @DViga it's the name of a local variable that represent the current element checked by the method, you need to have "some element called acc" that'll have the good ID – azro Jan 08 '19 at 22:21
  • Hello @azro. And removeIf? The program did not recognize. I think I'm not understanding. Can you explain me? I'm interested in understanding this code. – DViga Jan 08 '19 at 22:45
  • lambda expressions are not supported in -source 1.7 (use -source 8 or higher to enable lambda expressions)... @azro - return this message. – DViga Jan 08 '19 at 22:48
  • @DViga you can only use lambdas as of JDK8, seems like you're running on JDK7 which is why it will not work. – Ousmane D. Jan 08 '19 at 22:57
  • @azro it's worth noting that `removeIf` is not _short-circuiting_ and will remove _all_ matching items instead of just the first. if the studentId is unique (which I assume it's) then only `removeIf` will suffice in terms of removing the item but again not short-circuiting.... i.e. even after the item is removed it will traverse the entire list. – Ousmane D. Jan 08 '19 at 23:00
  • Oh. Sorry @Aomine, I did not know. I'm using this version, because that's what the teacher told us to use. This is old, but it's the one he uses at university. – DViga Jan 08 '19 at 23:03
1

Currently, you're using accountNumber as the index which is incorrect, instead loop over the list and find the index of the object and then remove:

for (int i = 0; i < linkedAccount.size(); i++) {
     if (linkedAccount.get(i).getStudentID() == accountNumber) {
         obj1 = linkedAccount.remove(i);
         break;
     }
}

Further, why are you using a LinkedList instead of an ArrayList? the latter is almost always favourable.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Hi @Aomine. Thank you for reply me and for helping me. I know ArrayList is easier to work with in this case. But, I'm doing it that way because I'm going to have to use a Queue in another part of the program. That was the data structure teacher you asked for. – DViga Jan 08 '19 at 22:37
  • @DViga Right, I see. since you're being given instructions by your tutor then It would be wise to keep following that. good luck! – Ousmane D. Jan 08 '19 at 22:39
  • Hello @Aomine. Dp you know Queue? – DViga Jan 09 '19 at 02:31
1

I think the best option is to search the account in the list by the studentID and then remove it.

public Account{
    private int studentID;
    private String StudentName;

    public int getStudentID() {
       return this.studentID;
    }
}

public static void main(String[] args){

   int accountNumber;

   LinkedList<Account> linkedAccount = new LinkedList<>();
   Account obj1;

   System.out.println("Type the acc number: ");
   accountNumber = in.nextInt();
   for (int i = 0; i < linkedAccount.size(); i++) {
      if (accountNumber == linkedAccount.get(i).getStudentID()) {
         System.out.println("The student " + linkedAccount.get(i).getStudentID() + " has been deleted");
         linkedAccount.remove(i);
         break; // This is to exit for loop, but if you want to delete every instance in the list with this ID you can skip this break
      }
   }
}
stftnk
  • 21
  • 3