1

I'm having some trouble with removing an item from an ArrayList, specifically removing an item which has been specified and passing the id of this item to the remove() function.

    DockService service = new DockServiceImpl();
    // This should remove the boat with the ID: NV 2345 KO
    service.removeBoat("NV 2345 KO");

    if (service.getDock().getDockedBoats().size() == 2) {
        System.out.println("Test 2 - Removing a boat: Passed.");
    } else {
        System.out.println("Test 2 - Removing a boat: Failed.");
    }

This is the ArrayList in a separate class

    public static List<Boat> createBoatList() {
    List<Boat> boatList = new ArrayList<>();

    Boat firstBoat = new Boat("NV 1234 VN", 1000);
    Boat secondBoat = new Boat("NV 2345 KO", 1500);
    Boat thirdBoat = new Boat("NV 3053 NZ", 2000);

    boatList.add(firstBoat);
    boatList.add(secondBoat);
    boatList.add(thirdBoat);

    return boatList;
}

This is the dock class which contains the get set methods

  public Dock(String name, int capacity) {
    this.name = name;
    this.capacity = capacity;
    this.dockedBoats = new ArrayList<>();
}
    public List<Boat> getDockedBoats() {
    return dockedBoats;
}

So in my removal method, i have tried this

private Dock dock;
public boolean removeBoat(String boatId) {
    System.out.println(">>> Deleting boat.");
    this.dock.getDockedBoats().remove(boatId);
    return false;
}

But the remove(boatId) does not remove the boat from the arraylist, any help would be greatly appreciated

4
  • 4
    You are storing Boat objects in your List - then you are trying to remove one Boat by passing a String - this will not work. You need to first find the matching Boat object given the parameter and then remove that object from the list Commented Feb 27, 2018 at 22:59
  • also, your method should do --> return this.dock.getDockedBoats().remove(boatId); instead of just return false; Commented Feb 27, 2018 at 23:00
  • After you remove the object you should have 2 left, not 3. Read docs.oracle.com/javase/7/docs/api/java/util/… Commented Feb 27, 2018 at 23:11
  • @TomRobertson, regardless, it is a mess. I did not downvote btw. I think you answer is fine Commented Feb 27, 2018 at 23:15

1 Answer 1

2

You are storing Boat objects in your List - then you are trying to remove one Boat by passing a String -> this will not work.

You need to first find the matching Boat object given the parameter and then remove that object from the list.

Something along the lines of:

public boolean removeBoat(String boatId) {
    System.out.println(">>> Deleting boat.");
    Boat b = findBoatById(boatId);

    if( b != null ){
       this.dock.getDockedBoats().remove(b);
       return true;
    }

    return false;
}

public Boat findBoatById(String boatId) {
    System.out.println(">>> Find boat from list.");

    for( Boat b : this.dock.getDockedBoats() ){
       if( boatId.equalsIgnoreCase( b.boatId ) ){
          return b;
       } 
    }

    return null;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Not a downvoter. Just a question. didn't you mean .remove(b) instead of .remove(boatId) as I am not sure if equals method is overridden to match boatId.
@Jimmy youre right, works perfectly once i corrected that, thanks
Thanks @Jimmy - I fixed the typo now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.