需要使用 toString 和 add/remove 元素打印 Arraylist
Need to Print an Arraylist using toString and also add/remove elements
我正在修改一个图书馆的代码,遇到了很多麻烦,所以我需要解决的第一件事是如何添加和删除书籍,同时还使用 tostring 打印当前和以前的内容阵列。它可以很好地打印它们,但不会删除它们。谁能告诉我为什么?当它打印出来时,我希望它只说一次标题,比如
作品名:枪手
三人画
不是:
Title:Gunslinger
作品名:三人画
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);
}
}
}
首先,您必须为 Library
class 实施 equals()
和 hashCode()
方法。然后你可以使用 books.remove(new Library("The Gunslinger"))
.
您需要先学习的东西很少。
- 您需要覆盖
Library
class 中的 equals()
和 hashcode()
方法,以便您的 class 可以轻松用于 equals()
由 ArrayList
操作。可以在此处找到示例 http://tutorials.jenkov.com/java-collections/hashcode-equals.html
- 您正在从
Library
的数组列表中删除 String
。这是行不通的,因为很明显,该数组列表中不存在字符串,而是 Library
class 的对象。要从该数组列表中删除一个对象,您需要编写类似 的内容
books.remove(new Library("The Gunslinger"));
但要使其生效,您需要实施上述第 1 点。
更多信息:Relationship between hashCode and equals method in Java
从满足某些条件的 collection 中删除 object 的一种简单方法是 removeIf
方法。
使用 Java 8 这看起来像:
class Book {
public String getTitle() {
...
}
}
List<Book> library;
library.removeIf(book -> book.getTitle().equals("The Gunslinger"));
这不需要您覆盖用于 collection 项目的 class 中的 equals
和 hashCode
,因为谓词明确声明了您要查找的条件。
如果条件确实表示两本书是同一本书,我只会建议覆盖 equals
。拥有相同的书名可能不是唯一的条件:同名的两本书可以有多个版本,甚至可以有不同的作者。
如果您的 remove
方法的目的确实是删除具有给定标题的所有书籍,那么我建议更改其名称(至 removeAllBooksWithTitle
)并使用 removeIf
如上所述.
只需重写 Library
class 中的 equals
方法,如下所示。
@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;
}
并在从 ArrayList
中删除元素时使用以下代码。
books.remove(new Library("The Gunslinger"));
这会起作用。
您不能从 ArrayList<Library>
中删除 String
元素。
有关详细信息,请参阅 this 问题。
让我们看看你在尝试什么。
- 您向
ArrayList
添加了 2 个对象
- 然后您尝试通过调用
remove(String s)
从 ArrayList
中删除其中一个对象。
首先,没有remove(String s)
。您的代码实际调用的是 remove(Object o)
。即使 String
是 Object
,此方法真正期望的 Object
是您的 Library
对象,这就是为什么您的答案表明您必须覆盖 equals()
和 hashCode()
另一种方法是使用您自己的自定义 class (Library
) 扩展 ArrayList class,它具有 Book
个对象,并实现 remove(String s)
.
至于 "Titles: " 在您的结果中只出现一次,@Override toString() 就可以了。
Library
扩展 ArrayList
:
的自定义 class
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
自定义 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;
}
}
用法:
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);
}
结果:
After add:
Title: The Gunslinger
The Drawing of the Three
After remove:
Title: The Drawing of the Three
我正在修改一个图书馆的代码,遇到了很多麻烦,所以我需要解决的第一件事是如何添加和删除书籍,同时还使用 tostring 打印当前和以前的内容阵列。它可以很好地打印它们,但不会删除它们。谁能告诉我为什么?当它打印出来时,我希望它只说一次标题,比如
作品名:枪手
三人画
不是: Title:Gunslinger
作品名:三人画
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);
}
}
}
首先,您必须为 Library
class 实施 equals()
和 hashCode()
方法。然后你可以使用 books.remove(new Library("The Gunslinger"))
.
您需要先学习的东西很少。
- 您需要覆盖
Library
class 中的equals()
和hashcode()
方法,以便您的 class 可以轻松用于equals()
由ArrayList
操作。可以在此处找到示例 http://tutorials.jenkov.com/java-collections/hashcode-equals.html - 您正在从
Library
的数组列表中删除String
。这是行不通的,因为很明显,该数组列表中不存在字符串,而是Library
class 的对象。要从该数组列表中删除一个对象,您需要编写类似 的内容
books.remove(new Library("The Gunslinger"));
但要使其生效,您需要实施上述第 1 点。
更多信息:Relationship between hashCode and equals method in Java
从满足某些条件的 collection 中删除 object 的一种简单方法是 removeIf
方法。
使用 Java 8 这看起来像:
class Book {
public String getTitle() {
...
}
}
List<Book> library;
library.removeIf(book -> book.getTitle().equals("The Gunslinger"));
这不需要您覆盖用于 collection 项目的 class 中的 equals
和 hashCode
,因为谓词明确声明了您要查找的条件。
如果条件确实表示两本书是同一本书,我只会建议覆盖 equals
。拥有相同的书名可能不是唯一的条件:同名的两本书可以有多个版本,甚至可以有不同的作者。
如果您的 remove
方法的目的确实是删除具有给定标题的所有书籍,那么我建议更改其名称(至 removeAllBooksWithTitle
)并使用 removeIf
如上所述.
只需重写 Library
class 中的 equals
方法,如下所示。
@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;
}
并在从 ArrayList
中删除元素时使用以下代码。
books.remove(new Library("The Gunslinger"));
这会起作用。
您不能从 ArrayList<Library>
中删除 String
元素。
有关详细信息,请参阅 this 问题。
让我们看看你在尝试什么。
- 您向
ArrayList
添加了 2 个对象
- 然后您尝试通过调用
remove(String s)
从ArrayList
中删除其中一个对象。
首先,没有remove(String s)
。您的代码实际调用的是 remove(Object o)
。即使 String
是 Object
,此方法真正期望的 Object
是您的 Library
对象,这就是为什么您的答案表明您必须覆盖 equals()
和 hashCode()
另一种方法是使用您自己的自定义 class (Library
) 扩展 ArrayList class,它具有 Book
个对象,并实现 remove(String s)
.
至于 "Titles: " 在您的结果中只出现一次,@Override toString() 就可以了。
Library
扩展 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
自定义 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;
}
}
用法:
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);
}
结果:
After add:
Title: The Gunslinger
The Drawing of the Three
After remove:
Title: The Drawing of the Three