在 Java 运行时创建变量

发布于 2024-10-01 03:05:45 字数 1431 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离去的眼神 2024-10-08 03:05:45

使用数组。在 Javascript 中,将 var results = [] 放在循环之前,并使用 results.push(value) 附加结果。在 Java 中,您需要使用 ArrayList。 (顺便说一下,这些是非常不同的语言。)

Use an array. In Javascript, place var results = [] before your loop and append results using results.push(value). In Java, you'll want to use an ArrayList. (Those are very different languages, by the way.)

残疾 2024-10-08 03:05:45

希望这是有道理的。

不幸的是,事实并非如此。

在 Java 中,动态创建变量是没有意义的。这是非常难做到的,一旦你做到了,它们就很难使用。 (相比之下,在 Javascript 中很容易做到......)

但是,这只是意味着您需要以不同的方式做您想做的事情。例如,以下代码在循环中进行计算,然后将结果保存在(现有的)ArrayList 变量中:

    List<Integer> results = ArrayList<Integer>();
    while (...) {
        // Do computation ...
        int result = ...
        results.add(result);
    }
    // Now we have all of the results in 'results'

或者,如果您想将每个结果绑定到不同的名称,您可以这样做像这样的东西:

    Map<String, Integer> results = HashMap<String, Integer>();
    while (...) {
        // Do computation ...
        String name = ...
        int result = ...
        results.put(name, result);
    }

Hopefully this makes sense.

Unfortunately, it doesn't.

In Java it makes no sense to create variables on the fly. It is extremely difficult to do, and once you have done it they are extremely difficult to use. (By contrast, it is easy to do in Javascript ...)

However, this just means that you need to do what you are trying to in a different way. For instance, the following does a computation in a loop and then saves the results in an (existing) ArrayList variable:

    List<Integer> results = ArrayList<Integer>();
    while (...) {
        // Do computation ...
        int result = ...
        results.add(result);
    }
    // Now we have all of the results in 'results'

Or, if you want to bind each of the results to a distinct name, you could do something like this:

    Map<String, Integer> results = HashMap<String, Integer>();
    while (...) {
        // Do computation ...
        String name = ...
        int result = ...
        results.put(name, result);
    }
情独悲 2024-10-08 03:05:45

以下是我实施并帮助我轻松修复解决方案的方法,没有太多障碍。

// 创建数组列表

List accountList = new ArrayList(); 




for(int k=0;k < counter;k++){
        accountList.add(k, (String)flowCtx.getValueAt("transitId"+m));
}

迭代循环并将对象添加到带有索引的数组列表中。

//在运行时借助索引检索对象

String a = accountList.get(i));

Following is the way that i have implemented and helped me to fix my solution easily without much hurdles.

// Creating the array List

List accountList = new ArrayList(); 




for(int k=0;k < counter;k++){
        accountList.add(k, (String)flowCtx.getValueAt("transitId"+m));
}

Iterating the loop and adding the objects into the arraylist with the index.

//Retrieving the object at run time with the help of the index

String a = accountList.get(i));
友欢 2024-10-08 03:05:45

不,Java 中不可能在运行时声明变量。但是java提供了java.util.map,可以像下面的例子一样使用它。我们可以假设键是变量名。

地图<字符串,对象> declareVariableRuntime= new HashMap(); declareVariableRuntime.put("variableName", new Object());

No, It is not possible to declare variables in java at runtime. But java provides java.util.map, which can be used like in the example below. We can assume that the key is the variable name.

Map<String, Object> declareVariableRuntime= new HashMap<String, Object>(); declareVariableRuntime.put("variableName", new Object());

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文