在Java中运行时创建变量 [英] Creating Variables at Runtime in Java

查看:117
本文介绍了在Java中运行时创建变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我有一个非常大的循环,我需要在每次迭代结束时创建一个新变量(整数)。这可能吗?我读到了一个ScriptEngineManager类,但我不确定这是否能够提供帮助。我想我可以创建一堆等于0的整数,但我不确定我需要创建一个新变量多少次(它取决于循环的条件)。希望这是有道理的。

In my code I have a pretty big loop and I need to create a new variable at the end of each iteration (integers). Is this possible? I read about a ScriptEngineManager class, but I'm not sure if this will be able to help. I suppose I could create a bunch of integers equal to 0, but I'm not exactly sure how many times I will need to create a new variable (it depends on the conditions of the loop). Hopefully this makes sense.

推荐答案

使用数组。在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.)

这篇关于在Java中运行时创建变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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