Reputation: 59
This method should add an entire array of books into the library object books array. The method must add the new array at the end of the current library and avoid overwriting any books already in the library. Make sure to include one copy of each book. [For simplicity no books added through this method will ever be duplicates of each other or previous books].
The method I tried to wrote hasn't been adding arrays correctly. Here's the shortened Library class.
public class Library {
Book [] books;
int [] copies;
int [] checkedOut;
int numBooks;
public Library() {
books = new Book[400];// Array to hold each book in element
copies = new int[400];// Array to hold number of "book" copies corresponding to the element in the books array.
checkedOut = new int[400];// Array to hold number of checked out books corresponding to the elemtn in the books array.
numBooks = 0;// Number of unique books.
}
public void addMultipleBooks( Book [] b ) {
for(int k = 0; k < b.length; k++) {
for(int i = 0; i < books.length; i++) {
if(books[i] == null) { numBooks = i; }
books[numBooks] = b[k];
copies[numBooks] = copies[numBooks] + 1;
}
}
}
}// End Library Class
addMultipleBooks should do the following:
1)Add each of the new books in the right spot in the array. 2)Set the number of copies to 1 for each book. 3)Update numBooks.
And here's the new array of book objects in the main I am trying to pass through the method.
Book [] buildLibrary = new Book[10];
buildLibrary[0] = new Book( "Ulysses", new Author("Joyce","James") );
buildLibrary[1] = new Book( "The Great Gatsby", new Author("Fitzgerald","F. Scott") );
buildLibrary[2] = new Book( "A Portrait of the Artist as a Young Man", new Author("Joyce","James") );
buildLibrary[3] = new Book( "Lolita", new Author("Nobokov","Vladimir") );
buildLibrary[4] = new Book( "Brave New World", new Author("Huxley","Aldous") );
buildLibrary[5] = new Book( "The Sound and the Fury", new Author("Faulkner","William") );
buildLibrary[6] = new Book( "Catch-22", new Author("Heller","Joseph") );
buildLibrary[7] = new Book( "Darkness at Noon", new Author("Koestler","Arthur") );
buildLibrary[8] = new Book( "Sons and Lovers", new Author("Lawrence","D.H.") );
buildLibrary[9] = new Book( "The Grapes of Wrath", new Author("Steinbeck","John") );
Without using arraylists you can use the following code, but I agree arraylists are the simplest way.
public void addMultipleBooks(Book [] b) {
for(int i = 0; i < b.length; i++) {
books[numBooks] = b[i];
copies[numBooks]++;
numBooks++;
}
}
Upvotes: 0
Views: 696
Reputation: 5474
Try modifying your code for adding this way :
for (int k = 0; k < b.length; k++) {
for (int i = 0; i < books.length; i++) {
if (books[i] == null) {
numBooks = i;
books[numBooks] = b[k];
copies[numBooks] = copies[numBooks] + 1;
break;
}
}
}
This way, the adding will only happen if the current slot of the book storage in your library is empty (null), then proceed to the next book item to be added (break out from searching of empty slot in library's book array since the current book to be stored has found its proper slot)
Upvotes: 0
Reputation: 8246
The size of Arrays cannot be changed so either you would need to use an ArrayList instead like Rohit Jain says as these can be expanded and shortened when you add and remove items.
Either that or you would need to create an entirely new array with a length one greater than the original array if you wanted to add an item to it, then move all the original array items into the new one and then the new item.
So yes, use an ArrayList.
Upvotes: 1