Arrays with different datatypes i.e. strings and integers. (Objectorientend)

后端 未结 5 1668
谎友^
谎友^ 2020-12-24 15:29

For example I have 3 books: Booknumber (int), Booktitle (string), Booklanguage (string), Bookprice (int).

Now, I

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-24 16:21

    Notice the repetition of Book in Booknumber (int), Booktitle (string), Booklanguage (string), Bookprice (int)- it screams for a class type.

    class Book {
      int number;
      String title;
      String language;
      int price;
    }
    

    Now you can simply have:

    Book[] books = new Books[3];
    

    If you want arrays, you can declare it as object array an insert Integer and String into it:

    Object books[3][4]
    

提交回复
热议问题