增加变量名? [英] Increment variable names?

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

问题描述

好的,对于我正在做的事情,我需要增加我的变量名称,例如int Taco1 = 23432 .....int Taco2 = 234235656 .....int Taco3 = 11111.......

Okay so for what I am doing i need to increment my variables name, so for example int Taco1 = 23432..... int Taco2 = 234235656..... int Taco3 = 11111.......

但是我需要它是一个像

 int X = 0;
 some method with loop or recursion()
 int Taco(X) = bla bla bla
 x++

试图让我的变量名自动命名,每次都增加 1,这样它们就不会覆盖自己.如果这是不可能的,那么我很抱歉.

Trying to get my variable names to auto name themselves incremented by 1 every time, so they don't overwrite themselves. If this is impossible then my apologies.

推荐答案

你不能在 Java 中这样做,更重要的是,你不想这样做,因为这不是 Java 的工作方式.事实上,变量名并不像你想象的那么重要,甚至在编译代码中几乎不存在.更重要的是,您能够以尽可能简单和可靠的方式获得对您的对象的引用.这可能涉及一个数组、一个 ArrayList(可能是您在这里想要的)、一个 LinkedList、一个 Map(例如 HashMap)、一个 Set 和其他类型的集合.

You can't do this in Java and more importantly, you don't want to do this as this isn't how Java works. In fact variable names aren't nearly as important as you think and hardly even exist in compiled code. What is much more important is that you are able to get a reference to your objects in as easy and reliable a way as possible. This can involve an array, an ArrayList (likely what you want here), a LinkedList, a Map such as a HashMap, a Set, and other types of collections.

例如:

List<Taco> tacoList = new ArrayList<Taco>();
for (int i = 0; i < MAX_TACOS; i++) {
   tacoList.add(new Taco(i));
}

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

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