具有不同数据类型(即字符串和整数)的数组. (Objectorientend) [英] Arrays with different datatypes i.e. strings and integers. (Objectorientend)
问题描述
例如,我有3本书:
Booknumber (int)
,Booktitle (string)
,Booklanguage (string)
,Bookprice (int)
.
For example I have 3 books:
Booknumber (int)
, Booktitle (string)
, Booklanguage (string)
, Bookprice (int)
.
现在,我想拥有一个名为books[3][4]
的数组.我正在获取通过setBooknumber
设置的数据,如下所示:
Book1.getBooknumber(), Book1.getBooktitle(),...,Book3.getBookprice().
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().
我怎么实现的:books[3][4] array
.
我不能称其为String books[][] = new String [3][4]
.因为我无法进入Booknumber (int)
.我既不希望Booknumber也不是String也不希望Bookprice.请问我怎么知道的?
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?
进一步详细说明.我有2个班级:book和bookUI.
To further elaborate it. I have 2 classes: book and bookUI.
书
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
}
}
推荐答案
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];
为了响应O.P.的困惑,Book应该是一个对象,而不是数组.每本书应该自己创建(通过适当设计的构造函数),然后添加到数组中.实际上,我不会使用数组,而是使用ArrayList.换句话说,您正在试图将数据强行放入不适合手头任务的容器中.
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.
我敢说,有50%的编程人员正在为您的数据选择正确的数据结构.如果有很好的结构选择,自然会遵循算法.
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.
正确完成后,您的UI类将如下所示: 将泛型添加到以下代码段中.
When properly done, you get your UI class to look like: 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);
等
现在您可以使用收藏夹"界面并执行类似的操作:
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);
这篇关于具有不同数据类型(即字符串和整数)的数组. (Objectorientend)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!