Java 如何将多个变量值存储到单个数组或列表中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32427465/
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
How can I store multiple variable values into a single array or list?
提问by N.Cre
So I have the below variables which are a mix of data types including integers, strings, doubles and arrays. I already have the methods required to fill each variable. All I need now is to gather all the variables into a single row of data, and than display that data in another class for later use. The data should be kept with same data type so I can use them in another class Also the same row of data need to be stored more than once. Below is a demonstration of how I want the data to be stored.
所以我有以下变量,它们是数据类型的混合,包括整数、字符串、双精度数和数组。我已经有了填充每个变量所需的方法。我现在需要的只是将所有变量收集到一行数据中,然后在另一个类中显示该数据以供以后使用。数据应该保持相同的数据类型,以便我可以在另一个类中使用它们同样的数据行需要存储多次。下面是我希望如何存储数据的演示。
Ticket No = 1, Ticket Type = "Lotto", Ticket Value = 2.50, Numbers = {1,2,3,4,5}
Ticket No = 2, Ticket Type = "Hymanpot", Ticket Value = 2.50, Numbers = {4,1,4,5,5}
Kindly provide me with the simplest solution as possible. Thanks.
请尽可能为我提供最简单的解决方案。谢谢。
int id;
String ticketType;
double value;
int[] numbers= new int[5];
回答by Makoto
A class abstraction, which would represent a Ticket
, would be the simplest way to go about this. This is the cleanest way to keep all of the information about a ticket together without having to (really) worry about the underlying data structures.
表示 a 的类抽象Ticket
将是解决此问题的最简单方法。这是将有关票证的所有信息保存在一起的最干净的方法,而不必(真正)担心底层数据结构。
public class Ticket {
private int id;
private String ticketType;
private double value;
private int[] numbers = new int[5];
// various methods like getId, getTicketType, setId, setTicketType
// would follow from here.
}
回答by Bobulous
You could use an ArrayList<? extends Object>
and then cast from Object
to the correct type for each item index. But this is ugly, and it's far better to create a Ticket
class which has class fields for each value which belongs to a ticket. Something like this:
您可以使用ArrayList<? extends Object>
然后将 fromObject
转换为每个项目索引的正确类型。但这很丑陋,最好创建一个Ticket
类,该类为属于票证的每个值都具有类字段。像这样的东西:
class Ticket {
int ticketNumber;
TicketType ticketType;
int ticketValueInPence;
List<Integer> ticketDrawNumbers;
enum TicketType {
LOTTO,
HymanPOT
}
}
Then you can define a constructor and getter methods within your new class, so that you can create a ticket with specific values, and then retrieve the values set for a particular ticket.
然后,您可以在新类中定义构造函数和 getter 方法,以便您可以创建具有特定值的票证,然后检索为特定票证设置的值。
Once you've got a custom type you can add objects of that type to a list with code like this:
获得自定义类型后,您可以使用如下代码将该类型的对象添加到列表中:
List<Ticket> ticketList = new ArrayList<>();
ticketList.add(anyTicket);
Far better than casting from Object
.
比从Object
.
回答by Arjun Panickssery
You should create a new class called Ticket. It would look something like this:
您应该创建一个名为 Ticket 的新类。它看起来像这样:
public class Ticket {
int id;
String ticketType;
double value;
int[] numbers;
public Ticket(){}
public Ticket(int id, String ticketType, double value, int[] numbers){
this.id = id;
this.ticketType = ticketType;
this.value = value;
this.numbers = numbers;
}
public int getId() {
return id;
}
public String getTicketType() {
return ticketType;
}
public double getValue() {
return value;
}
public int[] getNumbers() {
return numbers;
}
public void setId(int id) {
this.id = id;
}
public void setTicketType(String ticketType) {
this.ticketType = ticketType;
}
public void setValue(double value) {
this.value = value;
}
public void setNumbers(int[] numbers) {
this.numbers = numbers;
}
}
Then, in your main class, create an ArrayList in one of two ways. Using your examples . . .
然后,在您的主类中,以两种方式之一创建一个 ArrayList。使用你的例子。. .
//You can create a ticket like this
Ticket ticketOne = new Ticket();
ticketOne.setId(1);
ticketOne.setTicketType("Lotto");
ticketOne.setValue(2.50);
ticketOne.setNumbers(new int[]{1, 2, 3, 4, 5});
//Alternatively, you can do it all at once
Ticket ticketTwo = new Ticket(2, "Hymanpot", 2.50, new int[]{4,1,4,4,5});
ArrayList<Ticket> listOfTickets = new ArrayList<>();
listOfTickets.add(ticketOne);
listOfTickets.add(ticketTwo);
回答by errorist
import java.util.ArrayList;
import java.util.List;
public class Ticket {
private int id;
private String type;
private double value;
private int[] numbers;
public Ticket(int id, String type, double value, int[] numbers) {
this.id = id;
this.type = type;
this.value = value;
this.numbers = numbers;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Ticket no = " + id + ", ");
builder.append("Ticket type = " + type + ", ");
builder.append("Ticket Value = " + value + ", ");
builder.append("Numbers = {");
for (int i = 0; i < 5; i++)
builder.append(i != 4 ? numbers[i] + "," : numbers[i]);
builder.append("}");
return builder.toString();
}
public static void main(String[] args) {
List<Ticket> ticketList = new ArrayList<>();
ticketList.add(new Ticket(1, "Lotto", 2.50, new int[]{1,2,3,4,5}));
ticketList.add(new Ticket(2, "Hymanpot", 2.50, new int[]{4,1,4,5,5}));
for (Ticket t: ticketList) {
System.out.println(t);
}
}
}
Well, I kind of did everything for you, however as everyone mentioned it's actually very essential concept in object oriented programming. Creating new data types using classes just like structs in C. Therefore you can use this type in lists and arrays. I hope it is useful and you've learned something.
好吧,我为你做了一切,但是正如每个人都提到的,它实际上是面向对象编程中非常重要的概念。使用类创建新的数据类型,就像 C 中的结构一样。因此,您可以在列表和数组中使用这种类型。我希望它是有用的,你已经学到了一些东西。
Probably I should mention the toString() method. It's implicitly called when you call the System.out.println() method. In this way you can get a nice string representatin of your data type.
也许我应该提到 toString() 方法。当您调用 System.out.println() 方法时,它会被隐式调用。通过这种方式,您可以获得一个很好的数据类型的字符串表示。
回答by Abdul Hanan
You can use:
您可以使用:
String[][][][] ticketInfo;
and convert integer and double values back to int and double when needed, by using
并在需要时将 integer 和 double 值转换回 int 和 double,使用
Integer.parseInt(ticketInfo[?][?][?][?]) and Double.parseDouble(ticketInfo[?][?][?][?])