Search code examples
javaarraysfor-loopnested-for-loop

Escaping inner loop and go to the outer loop after action is done


There is a code for the BigestCountries class.

It consists of 2 arrays:

private String[][] biggestCountries; - holds country name and the continent, e.g. biggestCountries[CHINA][COUNTRY_NAME] = "China"; biggestCountries[CHINA][COUNTRY_CONTINENT] = "Asia";

private int[][] countryData; - holds populations and year founded, e.g. countryData[CHINA][COUNTRY_POPULATION] = 1433783686; countryData[CHINA][COUNTRY_AGE_FOUNDED] = 1949;
public String[] getCountriesFoundedBetween(int min, int max){
    int countriesMatched;
    countriesMatched = 0;  
    String[] countriesFoundedBetween;

    if(biggestCountries == null || biggestCountries.length == 0){
         return null;
    }

    for(int i = 0; i < biggestCountries.length; i++){
        if(countryData[i][COUNTRY_AGE_FOUNDED] >= min && countryData[i][COUNTRY_AGE_FOUNDED] <= max){

            System.out.println(String.format("%d %s", countryData[i][COUNTRY_AGE_FOUNDED], biggestCountries[i][COUNTRY_NAME]));
            countriesMatched++;
        }
    }

    if(countriesMatched > 0){
        countriesFoundedBetween = new String[countriesMatched];
    } else {
        return null;
    }

    for(int i = 0; i < biggestCountries.length; i++) { // outer loop for countries array length of NUMBER_OF_COUNTRIES

        String countryMatched = null;
        System.out.println("biggestCountries[i] " + biggestCountries[i][COUNTRY_NAME]);

        if(countryData[i][COUNTRY_AGE_FOUNDED] >= min && countryData[i][COUNTRY_AGE_FOUNDED] <= max){
            for(int j = 0; j < countriesFoundedBetween.length; j++){ // how to escape inner loop?

                countryMatched =  biggestCountries[i][COUNTRY_NAME];
                countriesFoundedBetween[j] = countryMatched;
                System.out.println("countriesFoundedBetween: " + countriesFoundedBetween[j] + "; biggestCountries[i][COUNTRY_NAME]: " + biggestCountries[i][COUNTRY_NAME]);

            }      
        }      
    }

    return countriesFoundedBetween;
}

Unfortunately, It cannot escape from the inner loop and re-writes the matched country to all rows of the newly-generated array. Result


Solution

  • The method getCountriesFoundedBetween() can be implemented differently, without the need for nested loops, as follows.

    private static String[] getCountriesFoundedBetween(int min, int max) {
        if (max < min) {
            throw new IllegalArgumentException("'max' less than 'min'");
        }
        String[] countriesFoundedBetween;
        int countriesMatched = 0;
        int[] indexes = new int[biggestCountries.length];
        if (biggestCountries != null && biggestCountries.length > 0) {
            for (int i = 0; i < biggestCountries.length; i++) {
                if(countryData[i][COUNTRY_AGE_FOUNDED] >= min &&
                   countryData[i][COUNTRY_AGE_FOUNDED] <= max) {
                    indexes[countriesMatched++] = i;
                }
            }
            countriesFoundedBetween = new String[countriesMatched];
            for (int i = 0; i < countriesMatched; i++) {
                countriesFoundedBetween[i] = biggestCountries[indexes[i]][COUNTRY_NAME];
            }
        }
        else {
            countriesFoundedBetween = new String[0];
        }
        return countriesFoundedBetween;
    }
    

    The above code also returns an empty array rather than null which is preferable for methods that return arrays.

    Here is a complete example using population to determine the biggest countries.

    public class Countrys {
        private static final int  CHINA      = 0;
        private static final int  INDIA      = 1;
        private static final int  U_S_A      = 2;
        private static final int  INDONESIA  = 3;
        private static final int  PAKISTAN   = 4;
        private static final int  BRAZIL     = 5;
        private static final int  NIGERIA    = 6;
        private static final int  BANGLADESH = 7;
        private static final int  RUSSIA     = 8;
        private static final int  MEXICO     = 9;
    
        private static final int  COUNTRY_NAME = 0;
        private static final int  COUNTRY_CONTINENT = 1;
        private static final int  COUNTRY_POPULATION = 0;
        private static final int  COUNTRY_AGE_FOUNDED = 1;
    
        private static int[][] countryData = new int[][]{{1_427_647_786, 1949},
                                                         {1_352_642_280, 1950},
                                                         {  328_239_523, 1776},
                                                         {  273_523_615, 1945},
                                                         {  220_892_340, 1947},
                                                         {  210_147_125, 1889},
                                                         {  206_139_589, 1960},
                                                         {  164_689_383, 1971},
                                                         {  144_384_244, 1991},
                                                         {  128_932_753, 1810}};
        private static String[][] biggestCountries = new String[][]{{"China"     , "Asia"},
                                                                    {"India"     , "Asia"},
                                                                    {"U.S.A."    , "North America"},
                                                                    {"Indonesia" , "Asia"},
                                                                    {"Pakistan"  , "Asia"},
                                                                    {"Brazil"    , "South America"},
                                                                    {"Nigeria"   , "Africa"},
                                                                    {"Bangladesh", "Asia"},
                                                                    {"Russia"    , "Europe"},
                                                                    {"Mexico"    , "North America"}};
    
        private static String[] getCountriesFoundedBetween(int min, int max) {
            if (max < min) {
                throw new IllegalArgumentException("'max' less than 'min'");
            }
            String[] countriesFoundedBetween;
            int countriesMatched = 0;
            int[] indexes = new int[biggestCountries.length];
            if (biggestCountries != null && biggestCountries.length > 0) {
                for (int i = 0; i < biggestCountries.length; i++) {
                    if(countryData[i][COUNTRY_AGE_FOUNDED] >= min &&
                       countryData[i][COUNTRY_AGE_FOUNDED] <= max) {
                        indexes[countriesMatched++] = i;
                    }
                }
                countriesFoundedBetween = new String[countriesMatched];
                for (int i = 0; i < countriesMatched; i++) {
                    countriesFoundedBetween[i] = biggestCountries[indexes[i]][COUNTRY_NAME];
                }
            }
            else {
                countriesFoundedBetween = new String[0];
            }
            return countriesFoundedBetween;
        }
    
        public static void main(String[] args) {
            String[] result = getCountriesFoundedBetween(1950, 1980);
            System.out.println(Arrays.toString(result));
        }
    }
    

    Running the above code produces the following output:

    [India, Nigeria, Bangladesh]