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

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

问题描述

好的,我正在做的事情,我需要增加我的变量名称,所以例如
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.......

但我需要它是一个变量,如

But instead i need it to be a variable like

 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,一个地图,如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天全站免登陆