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
ArrayList<Section> aMainSectio开发者_如何学Pythonn = 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.
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>
}
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. There is even a special operator for it: ++.
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:
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:
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
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. 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.
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.
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).
精彩评论