Code
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Population> provinces = new ArrayList<Population>();
provinces.add(new Population("BC", 100));
provinces.add(new Population("DC", 1100));
provinces.add(new Population("CC", 300));
provinces.add(new Population("AC", 200));
Population lowPop = getLowPop(provinces);
System.out.println(lowPop.getName() + lowPop.getPopulation());
}
public static Population getLowPop(ArrayList<Population> pop) {
Population startPop = pop.get(0);
for (int i = 1; i < pop.size(); i++) {
if (startPop.population > pop.get(i).population) {
startPop = pop.get(i);
}
}
return startPop;
}
}
Pop Class
Code
public class Population {
String name;
int population;
public Population(String name, int population) {
this.name = name;
this.population = population;
}
//Don't really need these get methods. Since you can directly reference
//using pop.name or pop.population
public int getPopulation() {
return population;
}
public String getName() {
return name;
}
}
Don't know if this is what you are looking for or not.
Output was BC100
I tend to use ArrayList when making an array of objects.
Pm me if this helped or not.
This post was edited by SEALs on Mar 22 2018 12:50pm