我正在研究一種方法(public Book[] getBooksWrittenBy(String authorLastName)
),該方法應按作者的姓氏返回書籍對象數組。我的老師通過電子郵件將其編碼爲正確的:檢查所有書籍是否在構造函數中,並且在構造函數中有20本書,但是當我嘗試在BlueJ項目中運行測試任務課程時,它說java.lang.ArrayIndexOutOfBoundsException: 0
。當我測試一個對象時,它總是顯示作者的0本書。任何人都可以看到爲什麼這種方法會有問題?它是以下課程中的最後一種方法:Book []:數組索引超出範圍
public class BookStore
{
private String storeName; //e.g. "Jason's New Books"
private Book[] inventory; //Array of Book objects
/**
* creates the inventory Array of 100 book object
* note that the list does not contain birth years nor death years for the authors, nor
* years published for the books...just use 2013 for all of these dates
* booklist[0] = new Book(new Author)(new Name("first", "last", "middle"), new Date(), new Date(),
* new Date(), "ULYSSES")
*/
public BookStore()
{
inventory = new Book[20];
Book b1 = new Book(new Author(new Name("James", "Joyce", " "), new Date(2013,1,1), new Date()),
new Date(), "ULYSSES");
inventory[0] = b1;
inventory[1] = new Book(new Author(new Name("F.", "Fitzgerald", "Scott"), new Date(2013,1,1), new Date()),
new Date(), "The Great Gatsby");
inventory[2] = new Book(new Author(new Name("James", "Joyce", " "), new Date(2013,1,1), new Date()),
new Date(), "A Portrait Of The Artist As A Young Man");
inventory[3] = new Book(new Author(new Name("Vladimir", "Nabokov", " "), new Date(2013,1,1), new Date()),
new Date(), "Lolita");
inventory[4] = new Book(new Author(new Name("Aldous", "Huxley", " "), new Date(2013,1,1), new Date()),
new Date(), "Brave New World");
inventory[5] = new Book(new Author(new Name("William", "Faulkner", " "), new Date(2013,1,1), new Date()),
new Date(), "The Sound and the Fury");
inventory[6] = new Book(new Author(new Name("Joseph", "Heller", " "), new Date(2013,1,1), new Date()),
new Date(), "Catch-22");
inventory[7] = new Book(new Author(new Name("Arthur", "Koestler", " "), new Date(2013,1,1), new Date()),
new Date(), "Darkness at Noon");
inventory[8] = new Book(new Author(new Name("D.", "Lawrence", "H."), new Date(2013,1,1), new Date()),
new Date(), "Sons and Lovers");
inventory[9] = new Book(new Author(new Name("John", "Steinbeck", " "), new Date(2013,1,1), new Date()),
new Date(), "The Grapes of Wrath");
inventory[10] = new Book(new Author(new Name("Malcolm", "Lowry", " "), new Date(2013,1,1), new Date()),
new Date(), "Under the Volcano");
inventory[11] = new Book(new Author(new Name("Samuel", "Butler", " "), new Date(2013,1,1), new Date()),
new Date(), "The Way of all Flesh");
inventory[12] = new Book(new Author(new Name("George", "Orwell", " "), new Date(2013,1,1), new Date()),
new Date(), "1984");
inventory[13] = new Book(new Author(new Name("Robert", "Graves", " "), new Date(2013,1,1), new Date()),
new Date(), "I, Claudius");
inventory[14] = new Book(new Author(new Name("Virginia", "Woolf", " "), new Date(2013,1,1), new Date()),
new Date(), "To the Lighthouse");
inventory[15] = new Book(new Author(new Name("Theodore", "Dreiser", " "), new Date(2013,1,1), new Date()),
new Date(), "An American Tragedy");
inventory[16] = new Book(new Author(new Name("Carson", "McCullers", " "), new Date(2013,1,1), new Date()),
new Date(), "The Heart is A Lonely Hunter");
inventory[17] = new Book(new Author(new Name("Kurt", "Vonnegut", " "), new Date(2013,1,1), new Date()),
new Date(), "Slaughterhouse-Five");
inventory[18] = new Book(new Author(new Name("George", "Orwell", " "), new Date(2013,1,1), new Date()),
new Date(), "Animal Farm");
inventory[19] = new Book(new Author(new Name("W.", "Maugham", "Somerset"), new Date(2013,1,1), new Date()),
new Date(), "Of Human Bondage");
}
/**
* creates the inventory Array of 100 Book object
* note that the list does not contain birth years nor death years for the authors, nor
* years published for the books...just use 2013 for all of these dates
* stores the storeName parameter in the storeName instance variable, but only if it
* does not equal "Taylor's Used Books". If the parameter is "Taylor's Used Books",
* store the name as "Jason's Used Books" instead.
* @param storeName - the name of the book store
*/
public BookStore(String storeName)
{
if(storeName.equals("Taylor's Used Books"))
{
this.storeName = storeName;
}else if(storeName.equals("Taylor's Used Books"))
{
storeName = "Jason's Used Books";
}else
{
this.storeName = storeName;
}
}
/**
* accessor that gets store name
* @return store name
*/
public String getStoreName()
{
return storeName;
}
/**
* mutator that sets store name
* @param - store name
*/
public void setStoreName(String storeName)
{
this.storeName = storeName;
}
/**
* returns the number of books written by an author whose last name is authorLastName
* use a for loop
* use .equalsIgnoreCase()
*/
public int howManyBooksDidThisAuthorWrite(String authorLastName)
{
int counter = 0;
for(int i = 0; i < inventory.length; i++)
{
if(inventory[i].getAuthorName().equalsIgnoreCase(authorLastName))
{
counter++;
}
}
return counter;
}
/**
* returns the full name of the author who wrote the book by this title
* returns null if there is no Book with this title, or if title is null of ""
* use a for loop
*/
public String getAuthorFullName(String title)
{
for(Book authorName : inventory)
if (authorName.getTitle() != null && authorName.getTitle().equalsIgnoreCase(title))
{
return authorName.getAuthor().getName().getFullName();
}
return null;
}
/**
* returns null if there is no Book written by an author with authorLastName as their
* last name
* otherwise, returns an Array of Book objects(hint: declare and return a local Book[]
* object containing all Book objects which were written by an author with this last name
* (e.g. Orwell has two books in the list: 1984 and ANIMAL FARM)
*/
public Book[] getBooksWrittenBy(String authorLastName)
{
int byAuthor = 0;
for(int i = 0; i < inventory.length; i++)
{
if(inventory[i].getTitle().equals(authorLastName))
{
byAuthor++;
}
}
Book[] matches = new Book[byAuthor];
int indexNewArray = 0;
for(int j=0; j < inventory.length; j++){
if(inventory[j].getTitle().equals(authorLastName))
{
matches[indexNewArray] = inventory[j];
indexNewArray++;
}
}
return matches;
}
這是一個很大的代碼,請給出更多的信息拋出異常。 –