0

I'm having trouble writing a for-each loop that searches the arraylist and returns the county's name within the continent that has the highest gdp. Here's my code for it right now. (ElementsList is the original ArrayList)

public Country highestGdp(String continent) {
    boolean flag;
    for (Country cont : ElementsList) {
        if (cont.getContinent().equals(continent)) {
            ArrayList<Country> TMP1 = new ArrayList<Country>();
            TMP1.add(cont);
            for (Country gdp : TMP1) {
                double max = 0;

                if (max < gdp.getGDP()) {
                    max = gdp.getGDP();

                }
                if (gdp.getGDP() == max) {
                    ArrayList<Country> TMP2 = new ArrayList<Country>();
                    TMP2.add(gdp);
                }
                return gdp;
            }
        }
    }
    return null;
}
1
  • You should try to do this in two separate loops or check for max in each time you find a new country. Don't need to have two nested loops. Commented Mar 29, 2018 at 21:25

2 Answers 2

1

Each time you find a country in the right continent, you can check to see if it is greater than the max so far. Don't need to loop through all of them each time.

public Country highestGdp(String continent) {
    boolean flag;
    Country maxCountry = null;
    for (Country cont : ElementsList) {
        if (cont.getContinent().equals(continent)) {
            if (maxCountry == null) maxCountry = cont;
            if (maxCountry.getGDP() < gdp.getGDP()) {
                maxCountry = cont;
            }
        }
    }
    return maxCountry;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Sorry for saying it but Your code is a little messy ;)

To shortly solve Your problem, try to move max declaration before the loop like this:

[...]
double max = 0; 
for(Country gdp : TMP1){
[...]

We can see that TMP2 is completely useless, remove it:

 // ArrayList<Country> TMP2 = new ArrayList<Country>();    
 // TMP2.add(gdp);

You create TMP1 list always with only 1 element and then iterate over it. This is also useless, You can do the code directly on the element You are adding to the list.

First iteration over ElementList is a list of Country elements, but the element You iterate is called cont (=continent) which is a Continent and not the Country. Is it intended to use Country class to cover both: Countries and Continents? Do You plan to have a tree structure like "Continents contains many Countries"?

Final code to solve problem from Your original question should be like this:

 public Country highestGdp(String continent){
    Country countryWithMaxGdp = null;
        for(Country cont: ElementsList ){
        if(cont.getContinent().equals(continent)){
           if(countryWithMaxGdp == null || countryWithMaxGdp.getGDP() < cont.getGDP()){
             countryWithMaxGdp = cont;
            }    
        }
    }
    return countryWithMaxGdp;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.