Question

Bit of context...

In my project I have one embedded for loop that outputs data whereby for each category show the item and within each item show its property so in reality the output I generated is 3 columns of data in the console (headings: Category/Item/Property) The for loop to show this data looks like this (Variables are set earlier on in the method):

for... (picks up each category)
    for...(picks up items in category)
        for (String propertyName : item.getPropertyNames()) { 
                out.println(category.getName() + "\t"
                        + itemDesc.getName() + "\tProperty:"
                        + propertyName);    
            }
     }
}

The purpose of the project is to provide a more dynamic documentation of the properties of set components in the system. (The /t making it possible to separate them in to individual columns on a console and even in a file in say an excel spreadsheet should I choose to set the file on the printstream (Also at the start of this method.))

The Problem

Now for the problem, after the for loops specified above I have generated another for loop separate from the data but shows the list of all the functions and operators involved in the components:

//Outside the previous for loops
    for (Function function : Functions.allFunctions) {
        out.println(function.getSignature());   
    }

What I want is to set this list as the 4th column but the positioning of the for loop and the way it is set leaves it fixed on the first column with the categories. I cant add it after property names as the functions are more generic to everything in the lists and there maybe repetitions of the functions which I am trying to avoid. Is there a way to set it as the forth column? Having trouble finding the sufficient research that specifies what I am looking for here. Hope this makes sense.

Was it helpful?

Solution

One solution, if the total amount of output is small enough to fit in memory, is to simply save all the data into an ArrayList of String, and output it all at the very end.

List<String> myList = new ArrayList<String>();

for... (picks up each category)
    for...(picks up items in category)
        for (String propertyName : item.getPropertyNames()) { 
                myList.add(category.getName() + "\t"
                        + itemDesc.getName() + "\tProperty:"
                        + propertyName);    
            }
     }
}

int i = 0;
// Here we assume that the total lines output by the previous set of loops is
// equal to the total output by this loop.
for (Function function : Functions.allFunctions) {
    out.println(myList.get(i) + "\t" + function.getSignature());   
    i++;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top