如何在 Java 中创建循环并递增索引以用作字符串的一部分?

发布于 2024-11-18 22:32:29 字数 775 浏览 2 评论 0原文

因此,对于我当前的程序,我目前正在这样做:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

雨后彩虹 2024-11-25 22:32:29

听起来您想这样做:

ArrayList<Section> aMainSection = new ArrayList<Section>();
for(int i = 0; i < 3; i++)
{
    Section aSection = new Section();
    aSection.setName("Document "+(i+1));
    aSection.setSection("Section "+(i+1));
    aSection.setText("Text "+(i+1));
    aMainSection.Add(aSection);
}

如果您事先不知道要执行多少次,请尝试以下操作:

ArrayList<Section> aMainSection = new ArrayList<Section>();
int sectionNumber = 1;
boolean done = false;
while(!done)
{
    Section aSection = new Section();
    aSection.setName("Document "+ sectionNumber);
    aSection.setSection("Section "+ sectionNumber);
    aSection.setText("Text "+ sectionNumber);
    aMainSection.Add(aSection);

    sectionNumber++;
    done = <put something interesting here>
}

Sounds like you want to do this:

ArrayList<Section> aMainSection = new ArrayList<Section>();
for(int i = 0; i < 3; i++)
{
    Section aSection = new Section();
    aSection.setName("Document "+(i+1));
    aSection.setSection("Section "+(i+1));
    aSection.setText("Text "+(i+1));
    aMainSection.Add(aSection);
}

If you don't know how many times you want to do it before hand try this:

ArrayList<Section> aMainSection = new ArrayList<Section>();
int sectionNumber = 1;
boolean done = false;
while(!done)
{
    Section aSection = new Section();
    aSection.setName("Document "+ sectionNumber);
    aSection.setSection("Section "+ sectionNumber);
    aSection.setText("Text "+ sectionNumber);
    aMainSection.Add(aSection);

    sectionNumber++;
    done = <put something interesting here>
}
别理我 2024-11-25 22:32:29

试试这个:

for (int i=1; i<4; ++i) {
    Section aSection = new Section();
    aSection.setName("Document " + i);
    aSection.setSection("Section " +i );
    aSection.setText("Text " +i);
}

Try this one:

for (int i=1; i<4; ++i) {
    Section aSection = new Section();
    aSection.setName("Document " + i);
    aSection.setSection("Section " +i );
    aSection.setText("Text " +i);
}
固执像三岁 2024-11-25 22:32:29

是的,在 Java 中您可以递增变量。甚至还有一个特殊的运算符:++。

Yes, in Java you can increment variables. There is even a special operator for it: ++.

深爱不及久伴 2024-11-25 22:32:29

无法在Java中操作变量的名称,因此不可能执行诸如创建一堆String之类的操作,其中每个变量的名称都附加有不同的数字。您可以通过使用对实际源代码文本进行操作的预处理器来将其组合在一起,但在这种情况下,这是非常不必要的,因为有一个更简单的解决方案。

当您需要像节一样按顺序创建一大组变量时,您可以使用像 ArrayList<> 这样的集合来存储它们并按数字访问它们:

    ArrayList<Section> aMainSection = new ArrayList<Section>();
    int NumberOfTimesYouWantToIncrement = 2;

    for (int i=1; i<NumberOfTimesYouWantToIncrement; i++) {
    Section aSection = new Section();
    aSection.setName("Document + i );
    aSection.setSection("Section" + i );
    aSection.setText("Text" + i );
    aMainSection.add( aSection ); //assuming your MainSection is supposed to contain the other sections
    }

这将创建一个 ArrayList< ;>为您提供的 Section 部分,然后您可以迭代以获取您创建的不同部分:

for (Section i: aMainSection) {
    //do something with that 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 Strings 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:

    ArrayList<Section> aMainSection = new ArrayList<Section>();
    int NumberOfTimesYouWantToIncrement = 2;

    for (int i=1; i<NumberOfTimesYouWantToIncrement; i++) {
    Section aSection = new Section();
    aSection.setName("Document + i );
    aSection.setSection("Section" + i );
    aSection.setText("Text" + i );
    aMainSection.add( aSection ); //assuming your MainSection is supposed to contain the other sections
    }

This will create an ArrayList<> of Sections for you that you can then iterate through to get the different sections you created:

for (Section i: aMainSection) {
    //do something with that section
}

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

生死何惧 2024-11-25 22:32:29

你想让我做什么 ?只需创建几个部分?

ArrayList<Section> aMainSection = new ArrayList<Section>();
int sectionsCount = 3;
for (int i=1; i<=sectionsCount; i++)
{
  Section aSection = new Section();
  aSection.setName("Document " + i);
  aSection.setSection("Section " + i);
  aSection.setText("Text " + i);
  aMainSection.add(aSection);
}

What do you want to do ? Just create a few of you Sections ?

ArrayList<Section> aMainSection = new ArrayList<Section>();
int sectionsCount = 3;
for (int i=1; i<=sectionsCount; i++)
{
  Section aSection = new Section();
  aSection.setName("Document " + i);
  aSection.setSection("Section " + i);
  aSection.setText("Text " + i);
  aMainSection.add(aSection);
}
花开柳相依 2024-11-25 22:32:29

如果缺少生成和编译代码(这是可能的,但我 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.

冧九 2024-11-25 22:32:29
final ArrayList<Section> list = new ArrayList<Section>(100);
for (int = 0; i < 100; i++)
{
    final Section s = new Section();
    s.setName(String.format("Document %d", i));
    s.setSection(String.format("Section %d", i));
    s.setText(String.format("Text %d", i));
    list.add(s);
 }

创建并添加 100 个部分。

final ArrayList<Section> list = new ArrayList<Section>(100);
for (int = 0; i < 100; i++)
{
    final Section s = new Section();
    s.setName(String.format("Document %d", i));
    s.setSection(String.format("Section %d", i));
    s.setText(String.format("Text %d", i));
    list.add(s);
 }

Creates and adds 100 sections.

粉红×色少女 2024-11-25 22:32:29

你不能。所有变量都必须显式键入并在编译时已知。使用数组(或其他一些集合,例如您在第一行声明的 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文