Java:枚举/构造函数

Java:枚举/构造函数,java,Java,我有以下代码: public class Item{ String name, color; int weight, volume, cost; public Item(String name, int weight, int volume, int cost, String color){ this.name= name; this.weight = weight; this.volume = volume; this.cost = cost; thi

我有以下代码:

public class Item{
String name, color;
int weight, volume, cost;

public Item(String name, int weight, int volume, int cost, String color){
    this.name= name;
    this.weight = weight;
    this.volume = volume;
    this.cost = cost;
    this.color = color;
}
public class Item{
int weight, volume, cost, value,
base, height;

public Item(int x, String variable) {
    //some code 
}
我使用它通过将数据加载到csv文件中来创建项目的ArrayList。我在数组中有一些项目,比如苹果、梨、面条等。目前我需要调用itemList.get(2)来查看这些项目。我希望能够调用itemList.APPLE或其他变体,这样我就不必总是知道与项目关联的索引号。有办法做到这一点吗?这似乎是一个枚举技巧

接下来,我有一个类似的类,但我想创建一个对象x:

项目x=新项目(200,重量)

x、 toString()。。。。。。。。。。。。。。。输出:重量=200,体积=0,成本=0,价值=0,基础=0,高度=0

根据该代码:

public class Item{
String name, color;
int weight, volume, cost;

public Item(String name, int weight, int volume, int cost, String color){
    this.name= name;
    this.weight = weight;
    this.volume = volume;
    this.cost = cost;
    this.color = color;
}
public class Item{
int weight, volume, cost, value,
base, height;

public Item(int x, String variable) {
    //some code 
}

在代码中,我将首先将所有变量设置为0,然后我需要使用字符串值来选择要向其中添加x的变量。我该怎么做?我可以使用一个大的if,if-else,if-else语句将x添加到相应的变量中,如果字符串为.equals(“_;”),但是我简化了变量;实际上大约有30个。我只需要做一个很长的if语句,还是有更好的方法?

首先,根据您使用的列表抽象,您可以指定将值放在哪里。如果您希望随时检索特定对象,请查看以下内容:


用映射替换30个变量,将每个名称映射到其值。

如果您询问在运行时动态创建
enum
s,答案是否定的。大量设置器有什么问题?