Java 具有不同数据类型的数组,即字符串和整数。(面向对象)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2015538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Arrays with different datatypes i.e. strings and integers. (Objectorientend)
提问by Grumpy ol' Bear
For example I have 3 books:
Booknumber (int)
, Booktitle (string)
, Booklanguage (string)
, Bookprice (int)
.
例如,我有 3 本书:
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().
现在,我想要一个名为books[3][4]
. 我通过setBooknumber
这样获取我设置的数据:Book1.getBooknumber(), Book1.getBooktitle(),...,Book3.getBookprice().
How do I realize this: books[3][4] array
.
我如何意识到这一点: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?
我不能叫它String books[][] = new String [3][4]
。因为我进不去Booknumber (int)
。我不希望 Booknumber 是 String 和 Bookprice。请问我是怎么认识的?
To further elaborate it. I have 2 classes: book and bookUI.
进一步细化它。我有 2 个类:book 和 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
}
}
采纳答案by Chris Cudmore
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.
编辑:为了回应 OP 的困惑, Book 应该是一个对象,而不是一个数组。每本书都应该自己创建(通过正确设计的构造函数),然后添加到数组中。事实上,我不会使用数组,而是使用 ArrayList。换句话说,您正在尝试将数据强制放入不适合手头任务的容器中。
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.
我敢说 50% 的编程是为您的数据选择正确的数据结构。如果有一个好的结构选择,算法自然会随之而来。
When properly done, you get your UI class to look like: Edit: Generics added to the following code snippet.
正确完成后,您的 UI 类将如下所示: 编辑:将泛型添加到以下代码片段中。
...
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:
现在您可以使用 Collections 界面并执行以下操作:
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);
回答by Chandra Patni
Notice the repetition of Book
in Booknumber (int), Booktitle (string), Booklanguage (string), Bookprice (int)
- it screams for a class type.
注意Book
in的重复Booknumber (int), Booktitle (string), Booklanguage (string), Bookprice (int)
- 它为类类型尖叫。
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:
如果你想要数组,你可以将它声明为对象数组Integer
并String
插入其中:
Object books[3][4]
回答by George Johnston
Why not create a class Bookwith properties: Number, Title, and Price. Then store them in a single dimensional array? That way instead of calling
为什么不创建一个具有属性的类Book:Number、Title 和 Price。然后将它们存储在一个一维数组中?这样而不是打电话
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.
在我看来,它会更易于管理和代码友好。
回答by GuruKulki
use object type ie Object books[3][4];
使用对象类型,即对象书[3][4];
回答by CCA
@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.
@NoCanDo:您不能创建具有不同数据类型的数组,因为 java 仅支持具有特定数据类型或对象的变量。当您创建一个数组时,您将一系列相似的变量组合在一起——几乎就像一个扩展变量。因此,所有变量必须是相同的类型。Java 无法区分变量的数据类型,除非您告诉它它是什么。例如:int
告诉你声明给它的所有变量都是数据类型int
。您可以做的是创建 3 个具有相应信息的数组。
int bookNumber[] = {1, 2, 3, 4, 5};
int bookName[] = {nameOfBook1, nameOfBook2, nameOfBook3, nameOfBook4, nameOfBook5}
// etc.. etc..
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.
现在,一个索引号为您提供了该书的所有信息。例如:索引号为 0 ([0]) 的所有数组都包含第 1 本书的信息。