使用2种方法1.检查对象是否存在2.添加对象

时间:2015-09-21 10:50:00

标签: java arrays methods

我想知道如何创建2种方法,  1.如果对象已存在于数组中(基于其中一个参数),则检查哪个。  2.其次添加一个方法,允许用户/我在前一个(第一个方法)返回false时将对象添加到数组中。

这是我的尝试:

public class Book {
    String name;
    int isbn;


    public Book(String e, int iNr) throws LibraryException{
        if(e.equals("")){
            throw new LibraryException("Blank / empty name is not allowed!");
        }

        else if(iNr < 1 || iNr > 9000){
            throw new LibraryException("The isbn number is outside the allowed range (1 - 9000)! ");
        }
        setName(e);
        setIsbn(iNr);
    }


    public boolean equals(Object obj){
        if(obj instanceof Book){
            isbn = (isbn)obj;
            return true;
        }
        return false;
    }


    public String getName() {
        if(name == null || name == ""){
            System.out.print("Does not exist!");
        }
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getIsbn() {
        if(isbn < 0){
            System.out.print("ISBN (LIBRI) EXCEPTION TO BE ADDED!");
        }
        return isbn;
    }

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

public class Library
{
    String name;
    Book[] books;
    int nrBooks = 0;

    public Library(String name, int nrBooks) throws LibraryException {  
        if(name.equals("")){
            throw new LibraryException("Blank names is not allowed");
        }

        if(nrBooks < 500){
            throw new LibraryException("The number of books needs to be greater than 500!");
        }

        this.name = name;
        this.nrBooks = nrBooks;
        this.books = new Book[nrBooks];

    }


    public void addBook(Book book) throws LibraryException {
         if(indexOf(book) == -1){
                if(nrBooks < books.length)
                    books[nrBooks++] = book;

         }
    }
    private int indexOf(Book book)throws LibraryException {

        if(nrBooks < Book[].length){

        }


        return -1;
    }

    public static void main(String[]args) throws LibraryException{
        Library b = new Library("Sami Frasheri", 700);
        b.addBook(new Book("Paul Colhen - Alchemist", 1));
        b.addBook(new Book("Paul Colhen - Winners Stand ALone", 2));
        b.addBook(new Book("Paul Colhen - The river Piedra I sat and I cried", 3));

        System.out.print(b);
    }
}

4 个答案:

答案 0 :(得分:0)

尝试ArrayList In Java

他们将能够解决您面临的几乎所有问题。请查看containsadd

请注意,您必须覆盖Book对象的hashCodeequals方法才能使其有效运作。

答案 1 :(得分:0)

我建议你使用Book的ArrayList。它将解决您的所有问题。

  public class Library {

    private String name;
    private ArrayList<Book> books;
    private int maxNumberOfBooks;

    public Library(String name, int maxNumberOfBooks) {
        this.name = name;
        this.maxNumberOfBooks = maxNumberOfBooks;
        this.books = new ArrayList<Book>();
    }


    public void addBook(Book book) {
        if (!books.contains(book)) {
            books.add(book);
        }
    }

}

如果您需要检查两个参数,请执行以下操作:

public void addBook(Book book) {
    for (Book b : books) {
        if (b.getName().equals(book.getName())) {
            return; // another book has the same name
        }
        if (b.getIsbn() == book.getIsbn()) {
            return; // another book has the same ISBN
        }
    }
    books.add(book);
}

答案 2 :(得分:0)

无法修改数组的大小。如果要添加元素,则必须实例化新数组。

public void addBook(Book book) {
    int oldN = books.length;
    books = Arrays.copyOf(books, oldN + 1);
    books[oldN] = book;
    return books;
}

此函数可以替换indexOf()。它会查找数组中的元素。

private boolean contains(Book book) {
    for (Book b : books) {
        if (b != null && book.equals(b))
            return true;
    }
    return false;
}

答案 3 :(得分:0)

我会建议这样的事情:

     public void addBook(Book book) throws LibraryException {
             if(indexOf(book) == -1){//book does not exist
                    if(nrBooks < (books.length-1)){
                        books[nrBooks] = book;
                        ++nrBooks;
                    } else {
                        throw new LibraryException("Not space for more books.");
                    }
             }
        }


     private int indexOf(Book book)throws LibraryException {
         for(int i=0;i<books.lenght;++i){
            if(books[i].getName().equals(book.getName()) && books[i].getIsbn==book.getIsbn){
              return i;
            }//if
         }//for
      //else
      return -1;
     }