使用给定的字符串参数搜索数组元素

时间:2013-11-13 22:31:03

标签: java arrays sorting methods collections

我正在尝试为BookCollection类编写一个findBook方法。它找到一本具有指定ISBN的书。我无法找到比较不同类型元素的方法。我们得到了这种方法:

public void changePrice(String isbn, double price){
    int index = findBook(isbn);                                                //CREATE FINDBOOK METHOD TO HELP THIS METHOD
    if( index == -1){
        throw new UnsupportedOperationException("BookNotFound");
    }
    collection[index].setPrice(price); 
}

但我很困惑为什么使用字符串参数对findbook方法进行整数索引的比较。基本上,我们有一个类型为Book []的集合,并且使用给定参数isbn,必须在该集合集中搜索该isbn。

这是对我到目前为止的粗略估计:

   //this method is a helper function
   private String findBook(int is){
      this.isbn = is;   
      for(int i = 0; i <= collection.length; i++){
         add(isbn);
      }  
   } 

我知道这种方法是错误的,但我在想办法写这篇文章时遇到了很多麻烦。如何使用字符串参数isbn搜索此Book []类型的集合?如果你们想要我的整个代码让我知道,我会发布它!

感谢无论谁帮忙!

@drorbs这是我的数据字段和构造函数:

  private int limit = 200;
   //Array of type book
   private int Book[];

   //actual size of collection, initialized to zero. Must never exceed limit
   private Book[] collection;    //collection is of book type    

   private int lastElement;

   //Constructor
   public BookCollection(int l){
      limit = l;
      int lastElement = 0;
         if(limit <= 200){
            Book[] collection = new Book[limit];
         } else{
            throw new UnsupportedOperationException("CannotExceedLimit");
           }   
      }

3 个答案:

答案 0 :(得分:1)

我是否理解正确,您想在书籍[]中搜索ISBN,并返回找到的书籍的索引,如果找不到书籍,则返回-1?

我不知道一本书的样子,但假设它有一个getISBN()方法,并假设集合是你要搜索的Book [],我会创建一个方法,如:

private int findBook( String isbn ) {
    int returnIndex = -1;
    for ( int index = 0; index < collection.length; index++ ) {
        if ( collection[ index ].equals( isbn ) {
            returnIndex = index;
            break;
    }
    return returnIndex;
}

答案 1 :(得分:1)

这种方法的天真实现是:

private int findBook(String isbn){
    // iterate all the Book elements in the collection array
    for(int i = 0; i <= collection.length; i++){
        // check if the current book isbn matches the one provided argument
        if (collection[i].getIsbn().equals(isbn))
            return i;
    }
    return -1;
}

请注意,如果要查找book索引并且isbn参数的类型为String,则方法返回类型应为int。
另外,我认为bookscollection更适合数组。

答案 2 :(得分:1)

import java.util.Arrays;
import java.util.List;

public class Library {

    public static void main(String[] args) {
        new Library();
    }

    private List<Book> collection;

    public Library() {
        collection = Arrays.asList(
            new Book("Foo", "000-0-00-000000-1", 0.0d),
            new Book("Bar", "000-0-00-000000-2", 0.0d),
            new Book("Baz", "000-0-00-000000-3", 0.0d)
        );

        Book b = collection.get(1);

        changePrice(b.getIsbn(), 3.50);

        System.out.println(b);
    }

    public int findBook(String isbn) {
        for (Book book : collection) {
            if (book.getIsbn().equals(isbn)) {
                return collection.indexOf(book);
            }
        }

        return -1;
    }

    public void changePrice(String isbn, double price) {
        int index = findBook(isbn);

        if (index < 0) {
            throw new UnsupportedOperationException("BookNotFound");
        }

        collection.get(index).setPrice(price);
    }

    public class Book implements Comparable<Book> {
        private String author;
        private String isbn;
        private double price;

        public Book() {
            this.author = "NOT FOUND";
            this.isbn = "000-0-00-000000-0";
            this.price = 0.0f;
        }

        public Book(String author, String isbn, double price) {
            this.author = author;
            this.isbn = isbn;
            this.price = price;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }

        public String getIsbn() {
            return isbn;
        }

        public void setIsbn(String isbn) {
            this.isbn = isbn;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        // You can use this to sort books.
        @Override
        public int compareTo(Book other) {
            return this.getIsbn().compareTo(other.getIsbn());
        }

        @Override
        public String toString() {
            return String.format("Book [author=%s, isbn=%s, price=$%.2f]",
                    author, isbn, price);
        }
    }
}