當我試圖返回索引時出現錯誤(必須返回一個int),我看不到我做錯了什麼。如何將Object數組索引與一個int進行比較,並返回索引號?如何返回Object數組的索引?
//x starts at 10000 because the left most number is assumed to be at least a 1.
/**
* Search for a book id within the Book object array
* @param Book - Array of objects with book id, title, isbn, author, and category
* @param numOfBooks - how many books are in the library
* @param myBookID - The key to search for
* @return the index of the array where the key matches
*/
public static int bookSearch (Object[] Book, int numOfBooks, int myBookID) {
for (int x = 10000; x <= numOfBooks; x ++)
if (Book[x].equals(myBookID))
return x;
}
在Java中,你可以通過Book.length得到數組的長度。在Java中,你應該使用小寫變量。 – m0skit0 2013-02-27 11:10:18
這就是我的老師用這些確切的變量名輸入指令的方式......不想違背它。 – trama 2013-02-27 11:15:57
我建議你換教練/課程。 – m0skit0 2013-02-27 12:05:23