我无法弄清楚为啥我的数组列表没有使用我所说的新值进行更新
Posted
技术标签:
【中文标题】我无法弄清楚为啥我的数组列表没有使用我所说的新值进行更新【英文标题】:I cannot figure out why my Array list is not updating with the new values that I am stating我无法弄清楚为什么我的数组列表没有使用我所说的新值进行更新 【发布时间】:2019-06-02 17:36:25 【问题描述】:如果我在问这个问题时犯了任何错误,请告诉我我知道这是一个专业论坛,这是我的第一篇文章。 因此,我正在尝试为我的 APCS 高中课程编写代码,但是遇到了问题。仅供参考,这里是我遇到问题的部分代码的说明 -
3.) 创建一个名为 updateLocation 的方法,将国家/地区添加到运行的城市(例如,英国伦敦)。打印列表。
我遇到的问题发生在第 3 步。我已经编写了 updateLocation 方法,并为该方法编写了 if 循环,以用 Country 替换已定义的位置,但是,它没有正确输出。
这是我的代码-
import java.util.ArrayList;
public class Runner
private double time;
private String atheleteName;
private String nationality;
private String date;
private String location;
public Runner(double time, String atheleteName, String nationality, String date, String location)
this.time = time;
this.atheleteName = atheleteName;
this.nationality = nationality;
this.date = date;
this.location = location;
public double getTime()
return time;
public String getAtheleteName()
return atheleteName;
public String getNationality()
return nationality;
public String getDate()
return date;
public String getLocation()
return location;
public void setTime(double time)
this.time = time;
public void setAtheleteName(String atheleteName)
this.atheleteName = atheleteName;
public void setNationality(String nationality)
this.nationality = nationality;
public void setDate(String date)
this.date = date;
public void setLocation(String location)
this.location = location;
public String toString()
return
time + atheleteName + nationality + date + location;
public static void main(String[] args)
ArrayList<Runner> runners = new ArrayList<>();
runners.add(new Runner( 9.58," Usain Bolt "," Jamaica "," 16 August 2009 "," Berlin "));
runners.add(new Runner( 9.69," Tyson Gray "," USA "," 20 September 2009 "," Shanghai "));
runners.add(new Runner( 9.69," Yohan Blake "," Jamaica "," 23 August 2012 "," Lausanne"));
runners.add(new Runner( 9.72," Asafa Powell "," Jamaica "," 02 September 2008 "," Lausanne "));
runners.add(new Runner( 9.78," Nesta Carter "," Jamaica "," 29 August 2010 "," Rieti "));
runners.add(new Runner( 9.79," Maurice Greene "," USA "," 16 June 1999 "," Athens "));
runners.add(new Runner( 9.79," Justin Gatlin "," USA "," 05 August 2012 "," London "));
runners.add(new Runner( 9.80," Steve Mullings "," Jamaica "," 04 June 2011 "," Eugene "));
runners.add(new Runner( 9.84," Donovan Bailey "," Canada "," 27 July 1996 "," Atlanta "));
runners.add(new Runner(9.84," Bruny Surin "," Canada "," 22 August 1999 "," Seville "));
System.out.println("\nOriginal Array- ");
System.out.println("");
System.out.println("Time(sec): Name: Nationality: Date: Location:");
System.out.println("---------------------------------------------------------------------------");
printRunners(runners);
updateLocation(runners);
System.out.println("\nUpdate Runners- ");
System.out.println("");
System.out.println("Time(sec): Name: Nationality: Date: Location:");
System.out.println("---------------------------------------------------------------------------");
printRunners(runners);
insertName(runners,10.49," Florence (G.) Joyner ","USA "," 06 July 1988 "," Indianapolis, Indiana ");
System.out.println("\nUpdate Runners- ");
System.out.println("");
System.out.println("Time(sec): Name: Nationality: Date: Location:");
System.out.println("---------------------------------------------------------------------------");
printRunners(runners);
replaceName(runners," Bruny Surin "," Carl Lewis ", 9.86 ," USA "," 23 August 1991 "," Tokyo, Japan ");
System.out.println("\nUpdate Runners- ");
System.out.println("");
System.out.println("Time(sec): Name: Nationality: Date: Location:");
System.out.println("---------------------------------------------------------------------------");
printRunners(runners);
public static void printRunners(ArrayList<Runner> runners)
for(Runner runner:runners)
System.out.println(runner);
public static void updateLocation(ArrayList<Runner> runners)
for(Runner runner : runners)
if(runner.getLocation().equalsIgnoreCase("Berlin"))
runner.setLocation("Berlin, Germany");
else if(runner.getLocation().equalsIgnoreCase("Shanghai"))
runner.setLocation("Shanghai, China");
else if(runner.getLocation().equalsIgnoreCase("London"))
runner.setLocation("London, England");
else if(runner.getLocation().equalsIgnoreCase("Athens"))
runner.setLocation("Athens, Greece");
else if(runner.getLocation().equalsIgnoreCase("Eugene"))
runner.setLocation("Eugene, South Africa");
else if(runner.getLocation().equalsIgnoreCase("Seville"))
runner.setLocation("Seville, France");
else if(runner.getLocation().equalsIgnoreCase("Lausanne"))
runner.setLocation("Lausanne, Spain");
else if(runner.getLocation().equalsIgnoreCase("Rieti"))
runner.setLocation("Rieti, Australia");
else if(runner.getLocation().equalsIgnoreCase("Atlanta"))
runner.setLocation("Atlanta, USA");
public static void insertName(ArrayList<Runner> runners, double time, String atheleteName, String nationality, String date, String location)
Runner runner = new Runner(time,atheleteName,nationality,date,location);
runners.add(runner);
public static void replaceName(ArrayList<Runner> runners, String searchName, String replaceName, double time, String nationality, String date, String location)
for(Runner runner: runners)
if(runner.getAtheleteName().equalsIgnoreCase(searchName))
runner.setTime(time);
runner.setAtheleteName(replaceName);
runner.setNationality(nationality);
runner.setDate(date);
runner.setLocation(location);
我希望我的结果是-
时间(秒):姓名:国籍:日期:地点:
9.58 Usain Bolt 牙买加 2009 年 8 月 16 日,德国柏林
9.69 Tyson Gray 美国 2009 年 9 月 20 日 中国上海
9.69 Yohan Blake 牙买加 2012 年 8 月 23 日,西班牙洛桑
9.72 Asafa Powell Jamaica 2008 年 9 月 2 日,西班牙洛桑
9.78 牙买加内斯塔卡特 2010 年 8 月 29 日 澳大利亚
9.79 Maurice Greene 美国 1999 年 6 月 16 日,希腊雅典
9.79 Justin Gatlin 美国 2012 年 8 月 5 日,英国伦敦
9.8 Steve Mullings 牙买加 2011 年 6 月 4 日 南非尤金
9.84 Donovan Bailey 加拿大 1996 年 7 月 27 日,美国亚特兰大
9.84 Bruny Surin 加拿大 1999 年 8 月 22 日 法国塞维利亚 ..................................................... ………………………………………………………………………………………………………… 但是,我的输出看起来像这样-
时间(秒):姓名:国籍:日期:地点:
9.58 Usain Bolt 牙买加 2009 年 8 月 16 日柏林
9.69 Tyson Gray 美国 2009 年 9 月 20 日上海
9.69 Yohan Blake 牙买加 2012 年 8 月 23 日洛桑
9.72 Asafa Powell 牙买加 2008 年 9 月 2 日洛桑
9.78 内斯塔·卡特牙买加 2010 年 8 月 29 日列蒂
9.79 Maurice Greene 美国 1999 年 6 月 16 日雅典
9.79 Justin Gatlin 美国 2012 年 8 月 5 日伦敦
9.8 Steve Mullings 牙买加 2011 年 6 月 4 日尤金
9.84 Donovan Bailey 加拿大 1996 年 7 月 27 日亚特兰大
9.84 布鲁尼苏林加拿大 1999 年 8 月 22 日塞维利亚
如果您对代码有任何疑问,请告诉我。
【问题讨论】:
您在updateLocation
方法中的所有if
检查都失败了,因为它们都用空格填充。如果您绝对需要填充空格,您可以在 updateLocation
方法中使用 runner.getLocation().trim()
。
【参考方案1】:
字符串" Berlin "
和"Berlin"
不相等(即使您忽略大小写比较它们。第一个包含前导和尾随空格。删除它们或将runner.getLocation()
替换为runner.getLocation().trim()
。
【讨论】:
【参考方案2】:将updateLocation
方法更新为:
public static void updateLocation(ArrayList<Runner> runners)
for(Runner runner : runners)
if(runner.getLocation().trim().equalsIgnoreCase("Berlin"))
runner.setLocation("Berlin, Germany");
else if(runner.getLocation().trim().equalsIgnoreCase("Shanghai"))
runner.setLocation("Shanghai, China");
else if(runner.getLocation().trim().equalsIgnoreCase("London"))
runner.setLocation("London, England");
else if(runner.getLocation().trim().equalsIgnoreCase("Athens"))
runner.setLocation("Athens, Greece");
else if(runner.getLocation().trim().equalsIgnoreCase("Eugene"))
runner.setLocation("Eugene, South Africa");
else if(runner.getLocation().trim().equalsIgnoreCase("Seville"))
runner.setLocation("Seville, France");
else if(runner.getLocation().trim().equalsIgnoreCase("Lausanne"))
runner.setLocation("Lausanne, Spain");
else if(runner.getLocation().trim().equalsIgnoreCase("Rieti"))
runner.setLocation("Rieti, Australia");
else if(runner.getLocation().trim().equalsIgnoreCase("Atlanta"))
runner.setLocation("Atlanta, USA");
搜索字符串包含空格,而 expected string
不包含空格
【讨论】:
以上是关于我无法弄清楚为啥我的数组列表没有使用我所说的新值进行更新的主要内容,如果未能解决你的问题,请参考以下文章
我正在尝试设置一些不同的多维数组,但无法弄清楚为啥这不起作用