-->

Arrays with different datatypes i.e. strings and i

2020-02-19 18:18发布

问题:

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

Now, I want to have an array called books[3][4]. I'm getting the data I set via setBooknumber like this:
Book1.getBooknumber(), Book1.getBooktitle(),...,Book3.getBookprice().

How do I realize this: books[3][4] array.

I can't call it String books[][] = new String [3][4]. Because I can't get Booknumber (int) into it. I don't want Booknumber to be String neither Bookprice. How do I realize it, please?

To further elaborate it. I have 2 classes: book and bookUI.

book

public class book{
String Booktitle, Booklanguage;
int Booknumber, Bookprice;

//constructor

//get

//set
}

bookUI

public class bookUI
{
 public static void main(String arg[])
 {
   book book1 = new book();
   book book2 = new book();
   book book3 = new book();

   book1.setBooktitle();
   ...
   book3.setBookprice();

   //Here I want to have books[3][4] Array. And gettin the data via book1.get...book3.get into the array
 }
}

回答1:

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

    // Add constructor, get, set, as needed.
}

then declare your array as:

Book[] books = new Book[3];

EDIT: In response to O.P.'s confusion, Book should be an object, not an array. Each book should be created on it's own (via a properly designed constructor) and then added to the array. In fact, I wouldn't use an array, but an ArrayList. In other words, you are trying to force data into containers that aren't suitable for the task at hand.

I would venture that 50% of programming is choosing the right data structure for your data. Algorithms naturally follow if there is a good choice of structure.

When properly done, you get your UI class to look like: Edit: Generics added to the following code snippet.

...
ArrayList<Book> myLibrary = new ArrayList<Book>();
myLibrary.add(new Book(1, "Thinking In Java", "English", 4999));
myLibrary.add(new Book(2, "Hacking for Fun and Profit", "English", 1099);

etc.

now you can use the Collections interface and do something like:

int total = 0;
for (Book b : myLibrary)
{
   total += b.price;
   System.out.println(b); // Assuming a valid toString in the Book class
}
System.out.println("The total value of your library is " + total);


回答2:

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]


回答3:

@NoCanDo: You cannot create an array with different data types because java only supports variables with a specific data type or object. When you are creating an array, you are pulling together an assortment of similar variables -- almost like an extended variable. All of the variables must be of the same type therefore. Java cannot differentiate the data type of your variable unless you tell it what it is. Ex: int tells all your variables declared to it are of data type int. What you could do is create 3 arrays with corresponding information.

int bookNumber[] = {1, 2, 3, 4, 5};
int bookName[] = {nameOfBook1, nameOfBook2, nameOfBook3, nameOfBook4, nameOfBook5} // etc.. etc..

Now, a single index number gives you all the info for that book. Ex: All of your arrays with index number 0 ([0]) have information for book 1.



回答4:

use object type ie Object books[3][4];



回答5:

Why not create a class Book with properties: Number, Title, and Price. Then store them in a single dimensional array? That way instead of calling

Book[i][j] 

..to get your books title, call

Book[i].Title

Seems to me like it would be a bit more manageable and code friendly.