0

I am tinkering with a code for a library and am having a lot of trouble so the first thing I need to sort out is how to add and remove books while also using a tostring to print the current and previous contents of the array. It is printing them out fine, but it isn't removing them. Could anyone let me know why? and when it prints them out I want it to only say title once like

title: The Gunlsinger

The Drawing of the three

not: Title:Gunslinger

Title:Drawing of the Three

class Library {

    private String books;

    Library(String b) {
        // this.owner=o;
        this.books = b;
    }

    //
    public String toString() {
        return "\nTitle:" + this.books;
        // System.out.println
    }

    public static void main(String[] args) {
        ArrayList<Library> books = new ArrayList<Library>();
        books.add(new Library("The Gunslinger"));
        books.add(new Library("The Drawing of the Three"));
        books.remove("The Gunslinger");
        for (Library a : books) {
            System.out.println(a);
        }

    }
}
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
Cori Sparks
  • 43
  • 1
  • 7

5 Answers5

1

Let's look at what you were trying.

  1. You added 2 objects to an ArrayList
  2. You then tried to remove one of the objects from the ArrayList by calling remove(String s).

First, there is no remove(String s). What your code is actually calling is remove(Object o). Even though a String is an Object, the Object this method really expects is your Library object, which is why you have answers that suggest that you have to override the equals() and hashCode()

Another approach would be to extend the ArrayList class with your own custom class (Library), that has Book objects, and implement a remove(String s).

As far as the "Titles: " only appearing once in your results, an @Override toString() is in order for that.

Library custom class that extends ArrayList:

public static class Library extends ArrayList<Book> {
    // Adding an overload remove method that accepts a String
    public void remove(String book) {
        // Find the book to remove
        for (int i = 0; i < size(); i++) {
            if (get(i).getTitle().equals(book)) {
                // This remove() is one of the default remove methods
                // that is part of an ArrayList
                remove(i);
                break;
            }
        }
    }

    // This will display "Titles: " once along with every
    // book in the ArrayList
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("Titles: ");
        // Append each book to the returning String
        forEach(book -> sb.append(book).append("\n"));
        return sb.toString();
    }
}

Book custom class:

public static class Book {
    private String title;

    public Book(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public String toString() {
        // You could append additional information like 
        // author, publisher, etc...
        return title;
    }
}

Usage:

public static void main(String[] args) throws Exception {
    Library books = new Library();
    books.add(new Book("The Gunslinger"));
    books.add(new Book("The Drawing of the Three"));
    System.out.println("After add: ");
    System.out.println(books);

    books.remove("The Gunslinger");
    System.out.println("After remove: ");
    System.out.println(books);      
}

Results:

After add: 
Title: The Gunslinger
The Drawing of the Three

After remove: 
Title: The Drawing of the Three
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

First you have to implement the equals() and hashCode() methods for your Library class. Then you can use books.remove(new Library("The Gunslinger")).

M. Shaw
  • 1,742
  • 11
  • 15
0

There are few things which you need to learn first.

  1. You need to override your equals() and hashcode() method in Library class, so your class can easily be used for equals() operation by the ArrayList. An example can be found here http://tutorials.jenkov.com/java-collections/hashcode-equals.html
  2. You are removing a String from an arraylist of Library. This won't work because , as obvious, strings are not present in that arraylist, rather objects of Library class. To remove an object from that arraylist, you need to write something like

books.remove(new Library("The Gunslinger"));

But for that to work, you need to implement point 1 above.

More information : Relationship between hashCode and equals method in Java

Community
  • 1
  • 1
Aakash
  • 2,029
  • 14
  • 22
0

An easy way to remove an object from a collection that satisfies some condition is the removeIf method.

Using Java 8 this would look something like:

class Book {
    public String getTitle() {
        ...
    }
}

List<Book> library;
library.removeIf(book -> book.getTitle().equals("The Gunslinger"));

This does not require you to override equals and hashCode in the class used for collection items as the predicate explicitly declares what condition you are looking for.

I would only recommend overriding equals if the condition really does represent two books being the same book. Having the same title is probably not the only condition: there can be multiple editions or even different authors for two books with the same title.

If the intention of your remove method is really remove all books with a given title then I would suggest changing its name (to removeAllBooksWithTitle) and using removeIf as discussed above.

sprinter
  • 27,148
  • 6
  • 47
  • 78
0

Just override equals method in your Library class as follows.

@Override
public boolean equals(Object obj) {
    if (obj == null) return false;
    if (obj == this) return true;
    if(obj instanceof Library) {
        Library lib = (Library) obj;
        return this.books.equals(lib.books);
    }
    return false;
}

and while removing the elements from ArrayList use the following code.

books.remove(new Library("The Gunslinger"));

this will work.

You cannot remove String element from ArrayList<Library>.

for more information see this question.

Community
  • 1
  • 1
ELITE
  • 5,815
  • 3
  • 19
  • 29