Java 我可以根据数组中的值命名变量吗?

Java 我可以根据数组中的值命名变量吗?,java,arrays,Java,Arrays,如果我有一个数组,我可以使用数组中的值来命名变量吗 到目前为止我已经试过了 String Statement = "Please enter the "; String[] Variables = {"Weight", "Length", "Height", "Width", "Zones"}; for (int x = 0; x <= 3; x++) { double Array.get(Variables, x) = UI.askDouble(Statement

如果我有一个数组,我可以使用数组中的值来命名变量吗

到目前为止我已经试过了

String Statement = "Please enter the ";
String[] Variables = {"Weight", "Length", "Height", "Width", 
"Zones"};


for (int x = 0; x <= 3; x++) {
        double Array.get(Variables, x) = UI.askDouble(Statement + Variables[x] + " (kg): ");
    }
String语句=“请输入”;
字符串[]变量={“重量”、“长度”、“高度”、“宽度”,
“区域”};

对于(int x=0;x我不知道您想要的是否可能,但映射是一种数据结构,它可能更符合您的想法:

Map<String, Double> vals = new HashMap<>();
vals.put("Weight", 100.5d);
vals.put("Length", 2.0d);
// now access those keys
System.out.println("Weight = " + vals.get("Weight");

然后,您可以使用此POJO作为包装,一次存储所有感兴趣的值。

可能的重复我认为可以将变量命名为数组中的值。
public class Measurements {
    private double weight;
    private double length;
    private double height;
    private double width;
    private double zones;

    // constructor and getters/setters
}