class PopulationUtils {

    public static Population[] appendPopulation(Population[] populations, Population populationToAppend, boolean appendAtTheEnd){
        Population[] result = null;
        if (populations != null){ // if there are populations to which to append
            if (populationToAppend != null){
                result = new Population[populations.length + 1];
                int startIndex = 0;
                if (appendAtTheEnd){
                    startIndex = 0;
                    result[result.length - 1] = populationToAppend;
                } else {
                    startIndex = 1;
                    result[0] = populationToAppend;
                }
                for (Population population : populations){
                    result[startIndex++] = population;
                }
            } else { // return original populations
                result = populations;
            }
        } else if (populationToAppend != null) { // return new array just of given populationToAppend
            result = new Population[]{populationToAppend};
        }
        return result;
    }

}