eclipse里arraylisthashmap报错

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了eclipse里arraylisthashmap报错相关的知识,希望对你有一定的参考价值。

参考技术A 1. 检查是否正确导入了相应的包;
2. 检查是否正确实例化了ArrayList和HashMap;
3. 检查是否正确使用了ArrayList和HashMap的方法;
4. 检查是否有语法错误。

ArrayListHashMap 与 员工类(程序员经理的结合使用) 相当于集合与继承的总结

  1 package Day28ketangzuoye;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.Map;
  6 
  7 public class EmployeeTest {
  8 
  9     public static void main(String[] args) {
 10         Emp se1=new SE(0001,"蔡正",true,6000,80);
 11         Emp se2=new SE(0002,"丁陆",true,7000,89);
 12         
 13         Emp pm1=new PM(1001,"王拥江",true,9000,"一年",3000);
 14         Emp pm2=new PM(1002,"贝杰明",true,13000,"七年",5000);
 15         
 16         System.out.println("ArrayList");
 17         ArrayList<Emp> list=new ArrayList<Emp>();
 18         list.add(se1);
 19         list.add(se2);
 20         list.add(pm1);
 21         list.add(pm2);
 22         SE e;
 23         PM m;
 24         for(Emp p:list){
 25             if(p instanceof SE){
 26                 e=(SE)p;
 27                 e.show();
 28             }else if(p instanceof PM){
 29                 m=(PM)p;
 30                 m.show();
 31             }else{
 32                 System.out.println("出错!");
 33             }
 34         }
 35         
 36         System.out.println("\n\n HashMap");
 37         Map<String,Emp> map=new HashMap<String,Emp>();
 38         map.put("蔡正",se1);
 39         map.put("丁陆",se2);
 40         map.put("王拥江",pm1);
 41         map.put("贝杰明",pm2);
 42         String[] str={"蔡正","丁陆","王拥江","贝杰明"};
 43         Emp p;
 44         for(int i=0;i<map.size();i++){
 45              p=map.get(str[i]);
 46             if(p instanceof SE){
 47                 e=(SE)p;
 48                 e.show();
 49             }else if(p instanceof PM){
 50                 m=(PM)p;
 51                 m.show();
 52             }else{
 53                 System.out.println("出错!");
 54             }
 55         }
 56         
 57     }
 58 }
 59 
 60 class Emp {
 61     public  int id;
 62     public  String name;
 63     public  boolean gender;
 64     public  double salary;
 65 
 66     
 67     public Emp() {
 68         super();
 69     }
 70 
 71     public Emp(int id, String name, boolean gender, double salary) {
 72         super();
 73         this.id = id;
 74         this.name = name;
 75         this.gender = gender;
 76         this.salary = salary;
 77     }
 78 
 79     public void show(){};
 80 }
 81 
 82 class SE extends Emp {
 83     int hot;
 84 
 85     public SE(int id, String name, boolean gender, double salary,int hot) {
 86         super(id,name,gender,salary);
 87         this.hot = hot;
 88     }
 89 
 90     @Override
 91     public void show() {
 92         System.out.println("SE [id=" + id + ", name=" + name + ", gender="
 93                 + gender + ", salary=" + salary + ", hot=" + hot + "]");
 94     }
 95 }
 96 
 97 class PM extends Emp{
 98     String exp;
 99     double bonus;
100     
101     
102     public PM(int id, String name, boolean gender, double salary,String exp, double bonus) {
103         super(id,name,gender,salary);
104         this.exp = exp;
105         this.bonus = bonus;
106     }
107 
108 
109     @Override
110     public void show() {
111         System.out.println("PM [id=" + id + ", name=" + name + ", gender="
112                 + gender + ", salary=" + salary + ", exp=" + exp + ", bonus=" + bonus + "]");
113     }
114 }
 1 ArrayList
 2 SE [id=1, name=蔡正, gender=true, salary=6000.0, hot=80]
 3 SE [id=2, name=丁陆, gender=true, salary=7000.0, hot=89]
 4 PM [id=1001, name=王拥江, gender=true, salary=9000.0, exp=一年, bonus=3000.0]
 5 PM [id=1002, name=贝杰明, gender=true, salary=13000.0, exp=七年, bonus=5000.0]
 6 
 7 
 8  HashMap
 9 SE [id=1, name=蔡正, gender=true, salary=6000.0, hot=80]
10 SE [id=2, name=丁陆, gender=true, salary=7000.0, hot=89]
11 PM [id=1001, name=王拥江, gender=true, salary=9000.0, exp=一年, bonus=3000.0]
12 PM [id=1002, name=贝杰明, gender=true, salary=13000.0, exp=七年, bonus=5000.0]

 

以上是关于eclipse里arraylisthashmap报错的主要内容,如果未能解决你的问题,请参考以下文章

java集合之ArrayListHashMap遍历

19.并发下的ArrayListHashMap,Integer加锁问题

eclipse 打开jsp页面报错

ArrayListHashMap 与 员工类(程序员经理的结合使用) 相当于集合与继承的总结

Eclipse的JSON文件报错解决

在接口的实现类里使用@Override注解报错