Java变量值作为新变量名 [英] Java variable value as new variable name

查看:539
本文介绍了Java变量值作为新变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在Java中是否有任何方法可以给新变量一个名称,该名称是另一个变量的值.以下代码是我要执行的操作的无效示例.

I was wondering if in Java there was any way to give a new variable a name which is the value of another variable. The below code is an non working example of what I'm trying to do.

int a = 0;
while(true){
    String (a) = "newValue";
    a = a + 1;
}

String b = "newVariableName";
int (b) = 1;

感谢您提供的任何帮助.

Thank for any help given.

推荐答案

您不能在Java中创建动态变量名称.我建议将这些变量放在以字符串为键的Map中.密钥可以是您想要的任何String.

You cannot create dynamic variable names in Java. I would recommend placing these variables in a Map with a string as a key. The key can be whatever String you would like.

这篇关于Java变量值作为新变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆