简体   繁体   English

Java: Object 阵列内 Object 阵列

[英]Java: Object Array within Object Array

I'm building a library program, which has 4 classes:我正在构建一个库程序,它有 4 个类:

  • Books ==> contains the title of a book Books ==> 包含一本书的标题
  • Genre ==> contains the name of a genre and an object array of books Genre ==> 包含一个流派的名称和一个 object 书籍数组
  • Library ==> contains an object array of genres库 ==> 包含一个 object 流派数组
  • App ==> contains the dialog and scanner App ==> 包含对话框和扫描仪

You'll be able to create new genre arrays and new book arrays from the app class.您将能够从应用程序 class 创建新类型 arrays 和新书 arrays。

public class App{
  Library library = new Library();

  //the other stuff

  private void run(){
     library.addGenres(insertGenreNameHere);
  }
}


public class Library{
   private Genres[] genres = new Genres[5];    //Obj. Array of genres
   private Int nrOfGenres = 0;                 //number of how many genres there are in an array

    public void addGenres(String genreName){   //adds a new genre to the array
       if (nrOfGenres < genres.length) {
        genres[nrOfGenres] = new Genres(genreName);
        nrOfGenres++;
    }
    else {
        System.out.println("You already have the maximum of " + genres.length + " genres!");
    }
}



public class Genres {

   private String name;
   private Books[] books = new Books[5];        //Obj. Array of books
   private int nrOfBooks = 0;                   //number of how many books there are in an array

   public Genres(String name) {                 //Constructor
       this.name = name;
   }

   //getter & setter for the name of the genre

   public void addBooks(String titel){          //adds new book to the array
      if (nrOfBooks < books.length) {
         books[nrOfBooks] = new Books(titel);
         nrOfBooks++;
    }
    else {
        System.out.println("You already have the maximum of " + books.length + " books!");
    }
}

public void showBooks(){                        //prints the books line by line
    int x = 0;
    while(x < books.length && books[x] != null) {
        System.out.println(books[x].getTitle());
        x++;
    }
  }
}



public class Books(){
   private String title;

   public Books(String title){                  //Constructor
      this.title = title;
   }

   //getter & setter for the title
}

However I yet don't know how I can add a book to its genre or even how I should "contact"(?) a book但是我还不知道如何将一本书添加到其类型中,甚至不知道我应该如何“联系”(?)一本书

If im correct I cant just do Genre genre = new Genre();如果我是正确的,我不能只做Genre Genre = new Genre(); or Books books = new Book();书籍书籍=新书籍(); because it has to be in an array (?)因为它必须在一个数组中(?)

I'd be very glad if one could help me and happy to share more information if needed如果有人可以帮助我,我会很高兴,如果需要,我很乐意分享更多信息

Cheers Martin干杯马丁

Are you sure that the number of books and number of genres will be constant?你确定书的数量和体裁的数量是不变的吗? I believe converting books and genres from arrays to ArrayList should help you achieve a smooth run of the program.我相信将书籍和流派从 arrays 转换为 ArrayList 应该可以帮助您顺利运行程序。 You do not need to keep a count on number of books and number of genres.您不需要计算书籍的数量和流派的数量。 Things can be much simpler when you use an ArrayList and its size() method.当您使用 ArrayList 及其 size() 方法时,事情会简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM