How can i write remove method using java?

Viewed 270

I need to remove the value . *see insert method - *

@Override
public void insertFaculty(Faculty e) {          
    setFaculty(e);

    list.add(e.getname());
    list.add(e.getfid());
    list.add(Double.toString(e.getsalary()));

    System.out.println("\n\nFaculty inserted sucessfully.");    
}

Now I need to remove the value that was input from my insert method *I have try removed method but does not work(⊙_⊙;) *

@Override
public void removeFaculty(Faculty e) {   
      int i;

      for(i = 1;i<list.size();i=i+1) {
          System.out.println(list.get(i));
          if(e.fid.equals(list.get(i))) {
              System.out.println("Match found .");
              list.remove(i-1);
              list.remove(i);
              list.remove(i+1);
          }
          else {
              System.out.println("Not found .");
          }
      }                     
  }
6 Answers

Consider that when you remove i-1, everything shifts along by 1. So, the thing that was previously at i is now at i-1. And if you remove that one, the thing initially at i+1 will then be at i-1.

So, you could remove i-1 3 times:

// Or use a for loop.
list.remove(i-1);
list.remove(i-1);
list.remove(i-1);

Or reverse the order of the removals:

list.remove(i+1);
list.remove(i);
list.remove(i-1);

Or clear a sublist:

list.subList(i-1, i+2).clear();

And don't forget to decrement i by one, so you will check the "new" thing at i on the next iteration (or break, if you only needed to find one thing).


All of this said, it looks like you are throwing related data of unrelated types into the same list.

You're also checking all elements in the list - but only one in three are ids. For one thing, you could just check element 1, 4, 7 etc. I don't know what the type of the id is, but in general you could find a value equal to the id in a "non-id position" (0, 2, 3, 5 etc), and removing that element and the adjacent elements would corrupt your list.

Instead of doing that, create a class to hold your three fields. Create an instance of that, and add it to the list (or just store your Faculty objects directly).

    @Override
    public void removeFaculty(Faculty e) {          
    setFaculty(e);

    list.remove(e.getname());
    list.remove(e.getfid());
    list.remove(Double.toString(e.getsalary()));
    System.out.println("\n\nFaculty Removed sucessfully.");    
}

You can use remove method list for removing. You need pass element which you want to remove.

Its better if you use list<Faculty> instead of list<String>.

List<Faculty> list= new ArrayList<Faculty>();

Hope, this will helps you.

There are different logical errors in your code:

  1. Your index should not be increased by 1, but by 3;
  2. When you remove items from the list, you don't have to increase the index.

Try this code:

@Override
public void removeFaculty(Faculty e) {
    int i = 1;

    while(i<list.size()) {
        System.out.println(list.get(i));
        if(e.fid.equals(list.get(i))) {
            System.out.println("Match found.");
            list.remove(i-1);
            list.remove(i-1);
            list.remove(i-1);
        } else {
            i+=3;
        }
    }
}
  1. If only 3 elements in your list then list size should be 3;

  2. list.get() method returns the element at the specified position in this list. but you didn't define the type of the list. So no idea which type of returns. I assumed that the type is Object

  3. if there is only one list then no need to call loop.

  4. Use the list as List<Faculty> rather than List<Object>


@Override 
public void removeFaculty(Faculty e) {   
          System.out.println(list.get(1));

          if(e.fid.equals(list.get(1)) {

              System.out.println("Match found .");
            list.subList(0, list.size()-1).clear();
         }
           else {
              System.out.println("Not found .");
          }


               }

Remove at index i twice and i-1 once, will remove the id, salary, and name.

After the first deletion, the element at index i+1 will be i sand will remain i-1

list.remove(i);
list.remove(i);
list.remove(i - 1);

The simple answer is that List.remove is shifting all the elements back to fill the hole left when you do list.remove(i-1), so the fix is to call list.remove(i-1) three times instead of calling it with i-1, i, and i+1.

But you really should be inserting the Faculty object into the list instead of its three fields independently. Use the generic List and list.add(e) in your insertFaculty method. Then a single call to list.remove in your removeFaculty method.

Related