如何在 Java 中创建循环并递增索引以用作字符串的一部分?
因此,对于我当前的程序,我目前正在这样做:
Java 代码
ArrayList<Section> aMainSection = new ArrayList<Section>();
Section aSection = new Section();
aSection.setName("Document 1");
aSection.setSection("Section 1");
aSection.setText("Text 1");
Section aSection2 = new Section();
aSection2.setName("Document 2");
aSection2.setSection("Section 2");
aSection2.setText("Text 2");
Section aSection3 = new Section();
aSection3.setName("Document 3");
aSection3.setSection("Section 3");
aSection3.setText("Text 3");
但我想要做的是创建一个 for 循环,其中当满足条件时,我可以创建一个新的部分。但是,我不知道如何在Java中增加变量。我认为这应该是可能的,但我知道这并不像将整数值连接到变量名的末尾那么简单。任何帮助将不胜感激,谢谢。
So for my current program, I am currently doing this:
Java Code
ArrayList<Section> aMainSection = new ArrayList<Section>();
Section aSection = new Section();
aSection.setName("Document 1");
aSection.setSection("Section 1");
aSection.setText("Text 1");
Section aSection2 = new Section();
aSection2.setName("Document 2");
aSection2.setSection("Section 2");
aSection2.setText("Text 2");
Section aSection3 = new Section();
aSection3.setName("Document 3");
aSection3.setSection("Section 3");
aSection3.setText("Text 3");
But what I want to be able to do is create a for loop in which when the condition is met, I can just create a new Section. However, I do not know how to increment variables in Java. I would assume it should be possible, somehow, but I know it's not as simple as concatenating an integer value to the end of the variable name. Any help would be appreciated, thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
听起来您想这样做:
如果您事先不知道要执行多少次,请尝试以下操作:
Sounds like you want to do this:
If you don't know how many times you want to do it before hand try this:
试试这个:
Try this one:
是的,在 Java 中您可以递增变量。甚至还有一个特殊的运算符:++。
Yes, in Java you can increment variables. There is even a special operator for it: ++.
您无法在Java中操作变量的名称,因此不可能执行诸如创建一堆
String
之类的操作,其中每个变量的名称都附加有不同的数字。您可以通过使用对实际源代码文本进行操作的预处理器来将其组合在一起,但在这种情况下,这是非常不必要的,因为有一个更简单的解决方案。当您需要像节一样按顺序创建一大组变量时,您可以使用像 ArrayList<> 这样的集合来存储它们并按数字访问它们:
这将创建一个 ArrayList< ;>为您提供的
Section
部分,然后您可以迭代以获取您创建的不同部分:这比操作变量名称要简单得多,因为它可以让您更轻松地创建和存储它们。想一想,如果您需要在 200 个不同的变量中创建 200 个部分,然后每当您想要循环遍历它们时再次通过名称提及所有这些部分,您需要做什么。 :D
You cannot manipulate the names of variables in Java, so it's impossible to do things like making a bunch of
String
s where each variable's name has a different number appended to it. You could hack this together by using a preprocessor that operates on actual source code text, but in this case that's highly unnecessary as there's a much simpler solution.When you need to create a large group of variables in sequential order like your sections, you can just use a collection like an
ArrayList<>
to store them and access them by number:This will create an ArrayList<> of
Section
s for you that you can then iterate through to get the different sections you created:This is a lot less cumbersome than manipulating the variable names as it lets you create and store them much more easily. Think about what you would have to do if you needed to create 200 sections in 200 different variables, and then mentioned all of them by name again whenever you wanted to loop through them. :D
你想让我做什么 ?只需创建几个部分?
What do you want to do ? Just create a few of you Sections ?
如果缺少生成和编译代码(这是可能的,但我 99% 确定您不想去那里),则无法完全按照您的要求进行操作。但我不相信你真的需要你所要求的。
考虑一下...稍后您打算如何处理变量 aSection2、aSection3...aSection974...。如果不生成相应的代码来使用它们,则无法使用它们。
相反,正如许多其他受访者的建议一样,使用集合。
Short of generating and compiling code (which is possible, but I'm 99% sure you don't want to go there) there's no way to do exactly what you asked for. But I don't believe you actually need to what you're asking for.
Consider this ... what did you plan to do with the variables aSection2, aSection3 ... aSection974 ... later. You can't use them without generating corresponding code to use them.
Instead use collections as many other respondents have suggestions.
创建并添加 100 个部分。
Creates and adds 100 sections.
你不能。所有变量都必须显式键入并在编译时已知。使用数组(或其他一些集合,例如您在第一行声明的 ArrayList)。
You can't. All variables must be explicitly typed and known at compile time. Use an array (or some other Collection like the ArrayList you declare on the first line).