如何将多个变量值存储到单个数组或列表中?

时间:2021-05-17 04:14:22

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 = "Jackpot", 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];

4 个解决方案

#1


4  

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 <?扩展Object>,然后从Object转换为每个项索引的正确类型。但这很难看,而且创建一个Ticket类要好得多,因为每个值都有一个类字段属于一个票证。像这样的东西:

class Ticket {
    int ticketNumber;
    TicketType ticketType;
    int ticketValueInPence;
    List<Integer> ticketDrawNumbers;

    enum TicketType {
        LOTTO,
        JACKPOT
    }
}

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转换好。

#2


2  

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.

一个类抽象,代表一个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.

}

#3


1  

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, "Jackpot", 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()方法时,它会被隐式调用。通过这种方式,您可以获得数据类型的良好字符串表示。

#4


0  

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, "Jackpot", 2.50, new int[]{4,1,4,4,5});

    ArrayList<Ticket> listOfTickets = new ArrayList<>();
    listOfTickets.add(ticketOne);
    listOfTickets.add(ticketTwo);

#1


4  

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 <?扩展Object>,然后从Object转换为每个项索引的正确类型。但这很难看,而且创建一个Ticket类要好得多,因为每个值都有一个类字段属于一个票证。像这样的东西:

class Ticket {
    int ticketNumber;
    TicketType ticketType;
    int ticketValueInPence;
    List<Integer> ticketDrawNumbers;

    enum TicketType {
        LOTTO,
        JACKPOT
    }
}

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转换好。

#2


2  

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.

一个类抽象,代表一个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.

}

#3


1  

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, "Jackpot", 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()方法时,它会被隐式调用。通过这种方式,您可以获得数据类型的良好字符串表示。

#4


0  

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, "Jackpot", 2.50, new int[]{4,1,4,4,5});

    ArrayList<Ticket> listOfTickets = new ArrayList<>();
    listOfTickets.add(ticketOne);
    listOfTickets.add(ticketTwo);