Java 退出内部循环,并在操作完成后转到外部循环
有一个用于BigestCountries类的代码 它由2个阵列组成:Java 退出内部循环,并在操作完成后转到外部循环,java,arrays,for-loop,nested-for-loop,Java,Arrays,For Loop,Nested For Loop,有一个用于BigestCountries类的代码 它由2个阵列组成: private String[][] biggestCountries; - holds country name and the continent, e.g. biggestCountries[CHINA][COUNTRY_NAME] = "China"; biggestCountries[CHINA][COUNTRY_CONTINENT] = "Asia"; private int[][] countryData; -
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[]getcountriesfoundbetween(int-min,int-max){
int countries匹配;
countriesMatched=0;
字符串[]countries在这两者之间进行了绑定;
if(biggestCountries==null | | biggestCountries.length==0){
返回null;
}
for(int i=0;i=min和&countryData[i][COUNTRY\u AGE\u FOUNDED]0){
CountriesFoundedBeween=新字符串[countriesMatched];
}否则{
返回null;
}
对于(int i=0;i 如果(countryData[i][COUNTRY\u AGE\u FOUNDED]>=min和&countryData[i][COUNTRY\u AGE\u FOUNDED]方法getCountriesFoundedBeween()
可以以不同的方式实现,而不需要嵌套循环,如下所示
私有静态字符串[]GetCountriesFoundedBeween(int-min,int-max){
如果(最大值<最小值){
抛出新的IllegalArgumentException(“最大值”小于“最小值”);
}
字符串[]countries在这两者之间进行了绑定;
int countriesMatched=0;
int[]索引=新的int[biggestCountries.length];
if(biggestCountries!=null&&biggestCountries.length>0){
for(int i=0;i=min&&
countryData[i][COUNTRY\u AGE\u FOUNDED]0){
for(int i=0;i=min&&
countryData[i][COUNTRY\u AGE\u成立]
如果你命名一个类BigestCountries
,它仍然有一个属性BigestCountries
,它是一个数组,保存着关于最大国家的信息,我认为你没有利用OO的潜力
您可以使用类来表示国家的数据
。通过数据封装,您可以摆脱嵌套数组,从而嵌套循环和控制流语句(中断、继续等),查找索引常量,保持最大国家
和国家数据
之间的索引同步
您的代码执行同一任务两次。第一个循环计算匹配的国家并初始化数组。第二个循环实际将匹配的国家名称放入数组
Java集合框架提供了动态大小的数据结构
我认为ageFound
应该命名为yearFound
重构你的代码
Country.java
public class Country {
private String name;
private int population;
private int yearFound;
private String continent;
public Country(String name, String continent, int year, int population) {
this.name = name;
this.population = population;
this.continent = continent;
this.yearFound = year;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public String getContinent() {
return continent;
}
public void setContinent(String continent) {
this.continent = continent;
}
public int getYearFound() {
return yearFound;
}
public void setYearFound(int yearFound) {
this.yearFound = yearFound;
}
@Override
public String toString() {
return "Country [name=" + name + ", population=" + population + ", yearFound=" + yearFound + ", continent="
+ continent + "]";
}
}
BiggestCountries.java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class BiggestCountries {
private List<Country> countries = new ArrayList<Country>() {
{
add(new Country("China", "Asia", 1949, 1433783686));
add(new Country("Canada", "North America", 1867, 37590000));
add(new Country("United States", "North America", 1776, 328200000));
}
};
public List<Country> getCountriesFoundedBetween(int min, int max) {
List<Country> matchedCountry = new ArrayList<Country>();
Iterator<Country> itrCoutnry = countries.iterator();
while (itrCoutnry.hasNext()) {
Country country = itrCoutnry.next();
int yearFound = country.getYearFound();
if (min < yearFound && max > yearFound) {
matchedCountry.add(country);
}
}
return matchedCountry;
}
}
你有三个循环,你想在最里面的循环中满足某些条件后跳转到最外面的循环?是的,有些国家的信息分布在两个不同的数组中。我需要遍历一个数组-捕获一些数据,然后遍历另一个数组,并将国家的名称添加到我的最后一个数组countriesfoundbeen
。在上面的屏幕截图上,长度是正确的-确实有6个国家符合标准。但ofc这些国家并不都是澳大利亚。为什么不在设置第一个嵌套循环可以测试的标志后从最里面的循环中断,如果为真,则中断到第一个循环?您只需要一个标记的中断
.好主意,但我认为我不知道如何正确实施。我尝试过这个方法,但似乎不起作用。
public class Country {
private String name;
private int population;
private int yearFound;
private String continent;
public Country(String name, String continent, int year, int population) {
this.name = name;
this.population = population;
this.continent = continent;
this.yearFound = year;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public String getContinent() {
return continent;
}
public void setContinent(String continent) {
this.continent = continent;
}
public int getYearFound() {
return yearFound;
}
public void setYearFound(int yearFound) {
this.yearFound = yearFound;
}
@Override
public String toString() {
return "Country [name=" + name + ", population=" + population + ", yearFound=" + yearFound + ", continent="
+ continent + "]";
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class BiggestCountries {
private List<Country> countries = new ArrayList<Country>() {
{
add(new Country("China", "Asia", 1949, 1433783686));
add(new Country("Canada", "North America", 1867, 37590000));
add(new Country("United States", "North America", 1776, 328200000));
}
};
public List<Country> getCountriesFoundedBetween(int min, int max) {
List<Country> matchedCountry = new ArrayList<Country>();
Iterator<Country> itrCoutnry = countries.iterator();
while (itrCoutnry.hasNext()) {
Country country = itrCoutnry.next();
int yearFound = country.getYearFound();
if (min < yearFound && max > yearFound) {
matchedCountry.add(country);
}
}
return matchedCountry;
}
}
public static void main(String[] args) {
List<Country> matchedCountries = new BiggestCountries().getCountriesFoundedBetween(1700, 1899);
System.out.println(matchedCountries);
}
[Country [name=Canada, population=37590000, yearFound=1867, continent=North America], Country [name=United States, population=328200000, yearFound=1776, continent=North America]]