问题描述
第一个类是食物类packageLineDistance;publicclassFood{privateStringname;privateintprice;publicFood(){super();}publicFood(Stringname,intprice){super();this.name=name;this.price=price;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetPrice(){returnprice;}publicvoidsetPrice(intprice){this.price=price;}@OverridepublicStringtoString(){return"您点了一份---"+name+"---价格是---"+price+"元";}publicStringtoString(intvv){return"厨师做好了一份---"+name+"---现在菜架上有"+vv+"菜";}publicStringtoString(intvo,intmv){return"服务员为您端来了一份---"+name+"---现在还有"+vo+"菜还没为您端过来--还有"+mv+"道菜未做完";}}下面是个方法类packageLineDistance;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Map;importjava.util.Random;importjava.util.Stack;publicclassMenu{privatestaticfinalMenuDo=null;staticMap<Integer,Food>is=newHashMap<Integer,Food>();staticint[]array=newint[10];staticintm=0;staticintsum=0;Foodp1=newFood("大白菜",10);Foodp2=newFood("大龙虾",50);Foodp3=newFood("回锅肉",30);Foodp4=newFood("麻婆豆腐",22);Foodp5=newFood("番茄鸡蛋",12);Foodp6=newFood("炒猪耳朵",26);Foodp7=newFood("宫爆鸡丁",35);Foodp8=newFood("干锅豆腐",18);Foodp9=newFood("烤羊串",3);Foodp0=newFood("萝卜汤",15);publicvoidshow(){is.put(1,p1);is.put(2,p2);is.put(3,p3);is.put(4,p4);is.put(5,p5);is.put(6,p6);is.put(7,p7);is.put(8,p8);is.put(9,p9);is.put(0,p0);}publicArrayList<Food>shoo(){ArrayList<Food>food=newArrayList<Food>();newMenu().show();Randomra=newRandom();for(inti=0;i<array.length;i++){intaa=ra.nextInt(10);array[i]=aa;food.add(is.get(aa));sum+=food.get(i).getPrice();System.out.println(food.get(i));}System.out.println("一共需要"+sum+"元");returnfood;}publicvoidDo(){ArrayList<Food>foodShelf=newArrayList<Food>();ArrayList<Food>food=newMenu().shoo();while(true){if(food.size()!=0){if(foodShelf.size()>=5){while(true){if(foodShelf.size()!=0){System.out.println(foodShelf.get(0).toString(foodShelf.size()-1,food.size()));foodShelf.remove(0);try{Thread.sleep(500);}catch(InterruptedExceptione){e.printStackTrace();}}else{break;}}}foodShelf.add(food.get(0));System.out.println(foodShelf.get(m).toString(foodShelf.size()));food.remove(0);m++;try{Thread.sleep(500);}catch(InterruptedExceptione){e.printStackTrace();}}else{System.exit(0);}}}}一个主程序packageLineDistance;publicclasslord{publicstaticvoidmain(String[]args){newMenu().shoo();newMenu().Do();}}在Do()方法第2个while循环跳出后要再进行添加食物的System.out.println(foodShelf.get(m).toString(foodShelf.size()));这行报错错误类型Exceptioninthread"main"java.lang.IndexOutOfBoundsException:Index:5,Size:1atjava.util.ArrayList.rangeCheck(ArrayList.java:635)atjava.util.ArrayList.get(ArrayList.java:411)atLineDistance.Menu.Do(Menu.java:72)atLineDistance.lord.main(lord.java:6)
解决方案
解决方案二:
packagetestString;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Map;importjava.util.Random;importjava.util.Stack;publicclassMenu{privatestaticfinalMenuDo=null;staticMap<Integer,Food>is=newHashMap<Integer,Food>();staticint[]array=newint[10];staticintm=0;staticintsum=0;Foodp1=newFood("大白菜",10);Foodp2=newFood("大龙虾",50);Foodp3=newFood("回锅肉",30);Foodp4=newFood("麻婆豆腐",22);Foodp5=newFood("番茄鸡蛋",12);Foodp6=newFood("炒猪耳朵",26);Foodp7=newFood("宫爆鸡丁",35);Foodp8=newFood("干锅豆腐",18);Foodp9=newFood("烤羊串",3);Foodp0=newFood("萝卜汤",15);publicvoidshow(){is.put(1,p1);is.put(2,p2);is.put(3,p3);is.put(4,p4);is.put(5,p5);is.put(6,p6);is.put(7,p7);is.put(8,p8);is.put(9,p9);is.put(0,p0);}publicArrayList<Food>shoo(){ArrayList<Food>food=newArrayList<Food>();newMenu().show();Randomra=newRandom();for(inti=0;i<array.length;i++){intaa=ra.nextInt(10);array[i]=aa;food.add(is.get(aa));sum+=food.get(i).getPrice();System.out.println(food.get(i));}System.out.println("一共需要"+sum+"元");returnfood;}publicvoidDo(){ArrayList<Food>foodShelf=newArrayList<Food>();//??????????ArrayList<Food>food=newMenu().shoo();while(true){if(food.size()!=0){if(foodShelf.size()>=5){//这里为0while(true){if(foodShelf.size()!=0){System.out.println(foodShelf.get(0).toString(foodShelf.size()-1,food.size()));foodShelf.remove(0);try{Thread.sleep(500);}catch(InterruptedExceptione){e.printStackTrace();}}else{break;}}}System.out.println("出错了");//=======================看这里============================foodShelf.add(food.get(0));//你在这里不停的取.取完数参数,却没做任何处理!取不到参数所以报空针异常.System.out.println(foodShelf.get(m).toString(foodShelf.size()));food.remove(0);m++;try{Thread.sleep(500);}catch(InterruptedExceptione){e.printStackTrace();}}else{System.exit(0);}}}}
看上面的注释!