So I have this code in my program, and there are multiple variations of it. Just looking at it, I feel like there must be a way to make it more efficient, but I can't think of anything. Just looking at it, does anyone have any ideas?
Essentially, what the code does is it determines which level of the text that it is currently parsing. If it it is in level 0, it adds that body of text to the aMainSection
ArrayList
.
If it it in level 1, it adds that body of text to aChildSection
of aMainSection
(An ArrayList
within the aMainSection
ArrayList
). Then it sets the aMainSection
text to 'parent text' within a new ArrayList
.
etc.
Notes:
aMainSection
&aChildSection
are arrayLists of typeSection
(A custom class)levelCount
is just an array of typeint
, size 10 (abstract number)
Code
// Adds the passed in heading text to the appropriate section
public static void addSectionText(String text)
{
if (currentLevel == 0)
{
aMainSection.get(levelCount[0] - 1)
.addSection(text);
}
else if (currentLevel == 1)
{
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.addSection(text);
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.getSection());
}
else if (currentLevel == 2)
{
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.addSection(text);
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.getSection());
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.getSection());
}
else if (currentLevel == 3)
{
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.aChildSection.get(levelCount[3] - 1)
.addSection(text);
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.aChildSection.get(levelCount[3] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.getSection());
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.aChildSection.get(levelCount[3] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.getSection());
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.aChildSection.get(levelCount[3] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.getSection());
}
else if (currentLevel >= 4)
{
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.aChildSection.get(levelCount[3] - 1)
.aChildSection.get(levelCount[4] - 1)
.addSection(text);
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.aChildSection.get(levelCount[3] - 1)
.aChildSection.get(levelCount[4] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.getSection());
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.aChildSection.get(levelCount[3] - 1)
.aChildSection.get(levelCount[4] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.getSection());
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.aChildSection.get(levelCount[3] - 1)
.aChildSection.get(levelCount[4] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.getSection());
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.aChildSection.get(levelCount[3] - 1)
.aChildSection.get(levelCount[4] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.aChildSection.get(levelCount[3] - 1)
.getSection());
}
}
Any help or thoughts would be appreciated.