trouble trying to sort objects in array
Hello. First, I apologise in advance if I'm not very clear. I'm taking my first Java class and my progress is not very good. I have an assignment that has plagued me all week and I'm not sure where to go. Before I begin I'd like to say I'm not looking for any easy solutions. I'm trying to code as much of this myself as I can. I'm just seriouslystuck.
I'm supposed to create an application that will 1. list individual items of an inventory, 2. alow a user to add/delete an item, and 3. search through the items by name. I've managed 1, I think I can do 2, but 3 is where I'm lost.
I've got a method that basically creates a record object of each item, which includes fields like item ID, item name, price, and publisher. I can see how to create an array of the objects, so I have something like the following:
Item book =new Item[5];
book[0] =new book(ID, name, price, publisher);
...
My problem is, once I create this array, how would I sort using only the name variable? Or any single variable for that matter, since I'm thinking, (but please correct me if I'm wrong), that because the number of an array has to be initialised first, for me to allow a user to add or delete an item will require writing a new array with the elements of the old one, and either adding the new item or else finding the ID of the one to be deleted andnot writing it in the new array.
This looks confusing and long even as I write it. Again, I'm really trying to figure most of it out myself, but any help on sorting through an array of objects like mentioned would be greatly appreciated. Thank you.
[1730 byte] By [
Arania] at [2007-11-26 19:33:52]

Rather than use an array, you might want to use a java.util.List, or even perhaps a java.util.Set, depending on what you're doing.
List implementations will handle things like reallocating storage. So you don't have to copy array contents around when you run out of space in the array.
The requirement "search through the items by name" sounds to me like it's saying that you need to retrieve an item by name. That's different from saying that you have to sort it.
Rather than sorting, you could (for example) create a java.util.Map, and have a mapping from names to book objects. Then retrieving an item by name can be done in one step.
Or you could just loop through an unsorted list or array. That's not terribly efficient but that's not a worry if there aren't a lot of books. Having the array be sorted is only necessary if you're going to do a binary search....and you lose any speed gains by doing that, if you re-sort the array for each lookup.
If you want to sort your array (or List), though, you can do that by either making it implement java.lang.Comparable; or, if you want to sort based on different critieria at different times, you can implement a bunch of java.util.Comparator objects. Then you can use java.util.Collections.sort or java.util.Arrays.sort to sort the array or List.
public class BookTest {
public static void main(String[] args) {
Book[] books = {
new Book(12, "The Red Canoe", 15.30, "Rockwell"),
new Book(92, "Fancy Pants",12.19, "Serrano"),
new Book(41, "Some Time",16.33, "Fantasy")
};
Inventory inventory = new Inventory(books);
inventory.listBooks();
inventory.add(new Book(16, "Yippie Yi", 15.12, "Stevens"));
inventory.listBooks();
inventory.sortByName();
inventory.listBooks();
inventory.remove(books[1]);
inventory.listBooks();
}
}
class Inventory {
Book[] books;
public Inventory(Book[] books) {
this.books = books;
}
public void sortByName() {
for(int j = 0, min = 0; j < books.length; j++) {
String name = books[j].name;
for(int k = j+1; k < books.length; k++) {
if(books[k].name.compareTo(name) < 0) {
min = k;
name = books[k].name;
}
}
// If we found a lower name, swap
if(!name.equals(books[j].name)) {
Book temp = books[min];
books[min] = books[j];
books[j] = temp;
}
}
}
public void add(Book book) {
Book[] temp = new Book[books.length+1];
System.arraycopy(books, 0, temp, 0, books.length);
temp[books.length] = book;
books = temp;
}
public void remove(Book book) {
Book[] temp = new Book[books.length-1];
for(int j = 0, k = 0; j < books.length; j++) {
if(book.name.equals(books[j].name))
continue;
temp[k++] = books[j];
}
books = temp;
}
public void listBooks() {
System.out.println("Inventory of Books:");
for(int j = 0; j < books.length; j++)
System.out.println("books["+j+"] = " + books[j]);
System.out.println("--");
}
}
class Book {
int ID;
String name;
double price;
String publisher;
public Book(int ID, String name, double price, String publisher) {
this.ID = ID;
this.name = name;
this.price = price;
this.publisher = publisher;
}
public String toString() {
return "Book[ID:" + ID + ",name:" + name + ",price:" +
price + ",publisher:" + publisher + "]";
}
}