简体   繁体   English

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

[英]How Do I Create a Loop in Java and Increment an Index to Use As Part of a String?

So for my current program, I am currently doing this:所以对于我目前的程序,我目前正在这样做:

Java Code 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");

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.但是我想要做的是创建一个 for 循环,当满足条件时,我可以创建一个新的部分。 However, I do not know how to increment variables in Java.但是,我不知道如何增加 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.我认为它应该是可能的,但我知道这并不像将 integer 值连接到变量名的末尾那么简单。 Any help would be appreciated, thank you.任何帮助将不胜感激,谢谢。

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.不能在 Java 中操纵变量的名称,因此不可能做一些事情,比如制作一堆String ,其中每个变量的名称都附加了不同的数字。 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<>这样的集合来存储它们并按数字访问它们:

    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 Section s for you that you can then iterate through to get the different sections you created:这将为您创建一个Section的 ArrayList<> ,然后您可以遍历它以获取您创建的不同部分:

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.想一想如果您需要在 200 个不同的变量中创建 200 个部分,然后在您想循环访问它们时再次按名称提及所有这些部分,您需要做什么。 :D :D

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);
}

Yes, in Java you can increment variables.是的,在 Java 中,您可以增加变量。 There is even a special operator for it: ++.甚至还有一个特殊的运算符:++。

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>
}
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.创建并添加 100 个部分。

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);
}

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.缺少生成和编译代码(这是可能的,但我 99% 确定你不想在 go 那里)没有办法完全按照你的要求去做。 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.考虑一下...稍后您打算如何处理变量 aSection2、aSection3...aSection974...。 You can't use them without generating corresponding code to use them.如果不生成相应的代码来使用它们,就无法使用它们。

Instead use collections as many other respondents have suggestions.而是使用 collections,因为许多其他受访者有建议。

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).使用数组(或其他一些集合,例如您在第一行声明的 ArrayList)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM