为了账号安全,请及时绑定邮箱和手机立即绑定

新手 java集合 练习中出现的bug问题 求大神解答 谢谢!!

新手 java集合 练习中出现的bug问题 求大神解答 谢谢!!

qq_Chrysler_0 2015-09-04 22:12:26
package com.imooc; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Util { public static List<ChinaPlayers> ListChinaPlayers; public static List<SpainPlayers> ListSpainPlayers; public static List<YourTeam> ListYourTeam; public Util(){ this.ListChinaPlayers = new ArrayList<ChinaPlayers>(); this.ListSpainPlayers = new ArrayList<SpainPlayers>(); this.ListYourTeam = new ArrayList<YourTeam>(); } public void add(){ ChinaPlayers p1 = new ChinaPlayers(1,"郜林    ", 29, "边锋",75 ); ListChinaPlayers.add(p1); ChinaPlayers p2 = new ChinaPlayers(2,"韦世豪", 22, "中锋", 75); ListChinaPlayers.add(p2); SpainPlayers p3 = new SpainPlayers(3,"伊斯科", 23, "前腰", 85, 1); ListSpainPlayers.add(p3); SpainPlayers p4 = new SpainPlayers(4,"莫雷诺", 21, "后卫", 81, 0); ListSpainPlayers.add(p4); } public static void testShow(){ Util util = new Util(); util.add(); System.out.println("  姓名:   年龄:  位置:  能力: 奖杯数:"); for (ChinaPlayers ChinaPlayer : ListChinaPlayers) { System.out.println(ChinaPlayer.getId()+"、"+ChinaPlayer.getName()+"      "+ChinaPlayer.getAge()+"    " +ChinaPlayer.getPosition()+"         "+ChinaPlayer.getAbility()); } for(SpainPlayers SpainPlayer :ListSpainPlayers){ System.out.println(SpainPlayer.getId()+"、"+SpainPlayer.getName()+"      "+SpainPlayer.getAge()+"    " +SpainPlayer.getPosition()+"         "+SpainPlayer.getAbility()+"    "+SpainPlayer.getCupNum()); } } public static void test2(){ Util util = new Util(); util.add(); Scanner console = new Scanner(System.in); int id = console.nextInt(); int i=0; while (i<ListChinaPlayers.size()) { if(id==(ListChinaPlayers.get(i).getId())){ YourTeam p1 = new YourTeam(ListChinaPlayers.get(i).getId(), ListChinaPlayers.get(i).getName(), ListChinaPlayers.get(i).getAge() ,ListChinaPlayers.get(i).getPosition(), ListChinaPlayers.get(i).getAbility(), 0); ListYourTeam.add(p1); System.out.println("成功交易"+ListYourTeam.get(i).getName()); break; }else if(id==(ListSpainPlayers.get(i).getId())){ YourTeam p1 = new YourTeam(ListSpainPlayers.get(i).getId(), ListSpainPlayers.get(i).getName(), ListSpainPlayers.get(i).getAge(),  ListSpainPlayers.get(i).getPosition(),  ListSpainPlayers.get(i).getAbility(), ListSpainPlayers.get(i).getCupNum()); ListYourTeam.add(p1); System.out.println("成功交易"+ListYourTeam.get(i).getName()); break; }else{ System.out.println("无此球员"); }i++; } } public void testShow2(){ System.out.println("当前球队名单:"); System.out.println("姓名:   年龄:  位置:  能力: 奖杯数:"); for (YourTeam YourTeam: ListYourTeam) { System.out.println(YourTeam.getName()+"      "+YourTeam.getAge()+"    " +YourTeam.getPosition()+"         "+YourTeam.getAbility());} } public void test(){ Util util = new Util(); System.out.println("欢迎买卖球员"); System.out.println("请问你确定要买卖球员吗(是:1/否:0)"); Scanner console = new Scanner(System.in); int s = console.nextInt(); if(s==0){ System.out.println("拜拜");} else if(s==1){ System.out.println("请选择您要交易的球员Id:"); Util.testShow(); Util.test2(); }else{ System.out.println("对不起,请重新输入"); util.test(); } } /**  * @param args  */ public static void main(String[] args) { Util util = new Util(); util.test(); util.testShow2(); } } public class Player { @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ability; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((position == null) ? 0 : position.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Player other = (Player) obj; if (ability != other.ability) return false; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (position == null) { if (other.position != null) return false; } else if (!position.equals(other.position)) return false; return true; } private  int id; private  String name; private  int age; private  String position; private  int ability; public Player(int id,String name,int age,String position,int ability){ this.id = id; this.name = name; this.age = age; this.position = position; this.ability = ability; } public  String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public int getAbility() { return ability; } public void setAbility(int ability) { this.ability = ability; } public int getId() { return id; } public void setId(int id) { this.id = id; }  } public class ChinaPlayers extends Player { public ChinaPlayers(int id,String name,int age,String position,int ability){ super(id,name,age,position,ability); } } public class SpainPlayers extends Player{ private int cupNum; public SpainPlayers(int id,String name, int age, String position, int ability,int cupNum) { super(id,name, age, position, ability); this.setCupNum(cupNum); // TODO Auto-generated constructor stub } public int getCupNum() { return cupNum; } public void setCupNum(int cupNum) { this.cupNum = cupNum; } } public class YourTeam extends Player { private int cupNum; public YourTeam(int id,String name,int age,String position,int ability,int cupNum) { super(id, name, age, position, ability); this.cupNum = cupNum; // TODO Auto-generated constructor stub } public int getCupNum() { return cupNum; } public void setCupNum(int cupNum) { this.cupNum = cupNum; } }当输入球员id2或4时,出现bugException in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.RangeCheck(ArrayList.java:547) at java.util.ArrayList.get(ArrayList.java:322) at com.imooc.Util.test2(Util.java:56) at com.imooc.Util.test(Util.java:95) at com.imooc.Util.main(Util.java:107)
查看完整描述

1 回答

?
历史与足迹

TA贡献6条经验 获得超6个赞

Util类中test2方法中的 System.out.println("成功交易"+ListYourTeam.get(i).getName());   list对象用错了,应该用,ListChinaPlayers.get(i).getName();你set数据的时候都没写错,打印的时候出的错。

查看完整回答
1 反对 回复 2016-01-13
  • 1 回答
  • 1 关注
  • 1568 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信