在变量中使用变量?

编程入门 行业动态 更新时间:2024-10-28 08:26:27
本文介绍了在变量中使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有几个字段,每个字段都是这样的:

I have several fields, each one is like this:

field1 field2 field3 ...

使用带计数器的循环,我希望能够说fieldx 。其中x是该循环中计数器的值。这意味着如果我的数组中有6个条目,则字段1 - 字段6将被赋予值。

Using a loop with a counter, I want to be able to say fieldx. Where x is the value of the counter in that loop. This means if I have 6 entries in my array, fields1 - field6 will be given values.

是否可以使用fieldx?

Is fieldx possible?

推荐答案

你可以用反射,但一般情况下,如果可以在数组中声明字段会更好。而不是:

You can do it with reflection, but in general it is better if you can declare your fields in an array. Instead of:

SomeType field1; SomeType field2; SomeType field3; ... SomeType field6;

你可以这样做:

SomeType[] fields = new SomeType[6];

然后你可以循环设置值的数组:

Then you can loop over the array setting the values:

for (int i = 0; i < fields.length; ++i) { fields[i] = yourValues[i]; }

更多推荐

在变量中使用变量?

本文发布于:2023-07-26 07:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1214852.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!