我正在尝试将“BookCollection”类型的两个对象数组合并到一个BookCollection数组中。我首先创建一个新的空白BookCollection,其大小相当于其他两个BookCollections的总和。
代码适用于第一个“collection1”:
public static BookCollection merge(BookCollection collection1, BookCollection collection2) {
BookCollection mergedCollection = new BookCollection(collection1.getSize() + collection2.getSize());
int collectionSize = 0;
// Add books from collection1 to mergedCollection.
// This loop works, 2/21/15, 3:30
for (int c1 = 0; c1 < collection1.getSize(); c1++) {
Book bookCopy = new Book(collection1.objectAt(c1));
mergedCollection.addBook(bookCopy);
collectionSize += 1;
}
现在我遇到了麻烦。我的目的是遍历下一个BookCollection(collection2),如果该书存在,只需更新价格和库存。如果它不存在,我想将该书添加到BookCollection。
// Loop through collection2
for (int c2 = 0; c2 < collection2.getSize(); c2++) {
// Create a copy of the current book.
Book bookCopy = new Book(collection2.objectAt(c2));
String copyIsbn = bookCopy.getIsbn();
// Loop through mergedCollection
for (int m = 0; m < mergedCollection.getSize(); m++) {
Book mergedBook = new Book(mergedCollection.objectAt(m));
String mergedIsbn = mergedBook.getIsbn();
// If the current book is already in mergedCollection.
// NOT GETTING THROUGH THE LOGIC HERE.
if (copyIsbn.equals(mergedIsbn)) {
// Variables to update values in mergedCollections
double price = bookCopy.getPrice();
int stock = bookCopy.getStock();
// If the mergedBook's price is larger than the collection's, change it.
if (mergedBook.getPrice() > price) {
mergedBook.setPrice(price);
}
// Update stock in mergedCollection
mergedCollection.changeStock(mergedBook.getIsbn(), bookCopy.getStock() + mergedBook.getStock());
// If the current book is not in mergedCollection.
} else {
mergedCollection.addBook(bookCopy);
collectionSize += 1;
}
}
}
return mergedCollection;
}
我试图对此进行评论,并且我认为问题所在的所有大写都有评论。现在我也认为知道这里使用的addBook()是相关的:
mergedCollection.addBook(bookCopy);
基本上说,如果我解析数组,并获得非null返回,那么我可以添加这本书。
现在,当我运行此代码时,编译器告诉我我正在尝试添加的书存在并停止。所以我知道a)addBook找到一个非null值,并且b)这一点逻辑:
if (copyIsbn.equals(mergedIsbn))
无法正常工作。
为什么我的程序没有捕获出现两次的Book对象并更新信息?
答案 0 :(得分:1)
我建议改用Set。让我举个例子:
让我们假设你的Book类是这样的:
class Book
{
private String bookNo;
private int price;
private int stock;
public Book(String bookNo, int price, int stock) {
this.bookNo = bookNo;
this.price = price;
this.stock = stock;
}
public String getBookNo() {
return bookNo;
}
public int getPrice() {
return price;
}
public int getStock() {
return stock;
}
@Override
public boolean equals(Object obj) {
if (obj != null && obj instanceof Book) {
if(this.bookNo.equals(((Book) obj).bookNo)) {
return true;
}
}
return false;
}
@Override
public int hashCode() {
return this.bookNo.hashCode();
}
}
现在要实现你想要的东西,你可以这样做:
Set<Book> hs1 = new HashSet<Book>();
Set<Book> hs2 = new HashSet<Book>();
hs1.add(new Book("book1", 10, 10));
hs1.add(new Book("book2", 20, 20));
hs2.add(new Book("book3", 30, 30));
hs2.add(new Book("book1", 40, 40));
hs2.addAll(hs1);
hs2拥有您想要的所需数据。
答案 1 :(得分:0)
如果您的目标是更新对象,而不是创建新实例:
Book mergedBook = new Book(mergedCollection.objectAt(m));
使用集合中已有的现有实例:
Book mergedBook = mergedCollection.objectAt(m);
然后对mergedBook所做的任何更新都将持久化到mergedCollection中的对象。
答案 2 :(得分:0)
好的,我弄清楚了。我一直在跑:
mergedCollection.addBook(bookCopy);
我所要做的就是创建一个:
found = false;
变量并将我的.addBook函数嵌入其中。