我现在收到错误BookCollection.java:67: error: incompatible types collection[lastElement++] = b;
也不确定我的构造函数是否设置正确?方向是:
构造函数:给定一个指定集合大小限制的参数,使用给定参数创建一个空书籍集合。该参数不应超过预设的最大大小 200。
那么我是否正确初始化了我的变量?下面的答案帮助我更改了我的代码,但是虽然我的构造函数中没有出现错误,但我觉得根据指示它可能不正确......
我将粘贴与该问题相关的几块代码。
public class BookCollection{
//data fields, need complete
private int limit = 200;
//Array of type book
private int Book[];
//actual size of collection, initialized to zero. Must never exceed limit
private int collection[];
private int lastElement;
//Constructor
public BookCollection(int l, int c[], int le,int b[]){
Book = b;
collection = c;
limit = l;
lastElement = le;
int lastElement = 0;
if(limit <= 200){
Book[] collection = new Book[limit];
} else{
throw new UnsupportedOperationException("CannotExceedLimit");
}
}
ANNDDDD 我收到错误的地方:
public void addBook(int b[], int c[]) {
Book = b;
collection = c;
if (lastElement == collection.length) {
throw new UnsupportedOperationException("CorrectionFull");
}
for (int i = 0 ; i != lastElement ; i++) {
if(b.equals(collection[i])) {
throw new UnsupportedOperationException("DuplicateBook");
}
}
collection[lastElement++] = b;
}