【问题标题】:[JAVA]How to add a element in a specific array[JAVA]如何在特定数组中添加元素
【发布时间】:2016-09-13 23:50:05
【问题描述】:

我正在尝试用 java 中的数组创建一个示例,我想创建一个图书馆,其中包含一些按类别排序的书籍,我想使用 addBook 方法将书籍添加到类别数组中,但我没有如果我有很多数组,我不知道该怎么做,目标是将书添加到相同的类别外壳中,我将 3 个类称为 Estante,Libros,libreria

package com.manzacatesSAS;

/**
 * Created by deborah on 7/09/16.
 */
public class Libreria    
{   
    Estante[] miedo     = new Estante[6];
    Estante[] comedia   = new Estante[6];
    Estante[] novelas   = new Estante[6];
    Estante[] romance   = new Estante[6];
    Estante[] comics    = new Estante[6];    
}

 package com.manzacatesSAS;   

/**
 * Created by deborah on 7/09/16.
 */
public class Estante {

    //attributes of Estante
    private int maxNumLibros = 6;
    //I guess this isn't necesay because if i use array,this only has 6 spaces
    private int maxPages = 3000;
    private String categoria = "";
    private int currentLibros = 0;
    private int currentPages = 0;

    public int getNumLibros() {
        return maxNumLibros;
    }

    public void setNumLibros(int maxNumLibros) {
        this.maxNumLibros = maxNumLibros;
    }

    public int getMaxPages() {
        return maxPages;
    }

    public void setMaxPages(int maxPages) {
        this.maxPages = maxPages;
    }

    public String getCategoria() {
        return categoria;
    }

    public void setCategoria(String categoria) {
        this.categoria = categoria;
    }

    /**
     * Constructor de Estantes
     *
     * @param categoria categoria de los libros
     */
    //No añadi los parametros restantes dado que al crear uno nuevo sus atributos deben estar vacios, o definidos por los atributos comunes.
    public Estante(String categoria) {
        this.categoria = categoria;
    }

        public void addBook(Libros x){
            if (currentPages + x.getNumPages() < maxPages && currentLibros < maxNumLibros) {
                currentLibros++;
            //I guess the array of the categorie to add the book would be here, and i think that use a for to add the book in the correct array.
            }
        }
    }

【问题讨论】:

  • 抱歉,不知道您要的是什么。
  • 所以你的图书馆有 5 个书架阵列,每个书架有 6 个书架的空间,总共 30 个书架。我期待在某处看到一系列书籍(Libros),但没看到?此外,一个数组留给您跟踪实际使用了多少空间,使用ArrayList 更容易,但当然,如果这是为了获得数组经验,它应该可以工作。
  • 不错的面向对象思维,顺便说一句。

标签: java arrays oop


【解决方案1】:

请参阅下面的类设计(如果您将类放在单独的文件中,请将它们设为public)。

在您的主程序中,您可以执行以下操作:

Library library = new Library();
Shelf shelf1 = new Shelf("Shelf 1");
library.addShelfToLibrary(shelf1);
Category fiction = new Category("fiction");
Category literature = new Category("literature");
Book book1 = new Book("Don Quixote");
book1.addBookToCategory(fiction);     // category for this book
book1.addBookToCategory(literature);  // another category for same book
shelf1.addBookToShelf(book1);

这是课程设计。您将需要添加更多方法,这是一个骨架。

class Library {
    private ArrayList<Shelf> shelves = new ArrayList<>();

    public void addShelfToLibrary(Shelf shelf) {
        if (!this.shelves.contains(shelf)) {
            this.shelves.add(shelf);
        }
    }
}

class Category {
    private String catname;

    public Category(String catname) {
        this.catname = catname;
    }

    public String getCatname() {
        return catname;
    }
}

class Book {
    private String title;
    private ArrayList<Category> categories = new ArrayList<>();

    public Book(String title) {
        this.title = title;
    }

    public ArrayList<Category> getCategories() {
        return categories;
    }
    public void addBookToCategory(Category category) {
        if (!this.categories.contains(category)) {
            this.categories.add(category);
        }
    }
}

class Shelf {
    private String shelfId;
    private ArrayList<Book> booksOnShelf = new ArrayList<>();

    public Shelf(String shelfId) {
        this.shelfId = shelfId;
    }

    public void addBookToShelf(Book book) {
        if (!this.booksOnShelf.contains(book)) {
            this.booksOnShelf.add(book);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 2021-05-07
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多