java中的addelement,[JAVA]如何在特定數組中添加元素。

I'm trying to make a example with arrays in java, I want to do a library with some books sorted in categories, I want to use a addBook method to add the book in the categories array but I don't know how do it if I've many arrays, the goal is add the book in the same shell of categories, I'd 3 class called Estante,Libros,libreria.

我試圖用數組的例子在java中,我想做一個圖書館和一些書籍分類類別,我想使用一個用於將方法添加類別的書數組但我不知道怎么做如果我多數組,我們的目標是添加相同的殼類的書,我叫Estante 3類,西西里,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.

}

}

}

1 个解决方案

#1

0

See class design below (if you put the classes in separate files make them public).

請參見下面的類設計(如果將類放在單獨的文件中,則將它們公開)。

In your main program you can do stuff like:

在你的主程序中,你可以做如下的事情:

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);

Here is the class design. You will need to add more methods, this is a skeleton.

這是課堂設計。您需要添加更多的方法,這是一個框架。

class Library {

private ArrayList 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 categories = new ArrayList<>();

public Book(String title) {

this.title = title;

}

public ArrayList getCategories() {

return categories;

}

public void addBookToCategory(Category category) {

if (!this.categories.contains(category)) {

this.categories.add(category);

}

}

}

class Shelf {

private String shelfId;

private ArrayList booksOnShelf = new ArrayList<>();

public Shelf(String shelfId) {

this.shelfId = shelfId;

}

public void addBookToShelf(Book book) {

if (!this.booksOnShelf.contains(book)) {

this.booksOnShelf.add(book);

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值