IDecideAll 2023-03-27 20:44 采纳率: 100%
浏览 125
已结题

java程序使用ArrayList时报错:Index 0 out of bounds for length 0,如何解决?

【java报错:Index 0 out of bounds for length 0】运用ArrayList类就出现了这种情况(编译器为Eclipse):

img

源码:

import java.util.Scanner;
import java.util.ArrayList;

public class Main {
    
    public static void main(String[] args) {
               String[] AllDishs_names = new String[] {"番茄炒蛋","酸辣土豆丝","红烧牛蛙","油淋生菜","虫草花排骨汤"};  //设置菜单
               int[] AllDishs_price = new int[] {15,10,35,10,35};       //设置单价
               ArrayList<Dish> AllDishs = new ArrayList<Dish>();
               int i = 0;
               for(String name : AllDishs_names) {
                   AllDishs.get(i).name = name;
                   i++;
               }
               int j = 0;
               for(int price : AllDishs_price) {
                   AllDishs.get(j).price = price;
                   j++;
               }                                            //13~21行设置菜品信息(包含菜名及其单价)
               Scanner input = new Scanner(System.in);
               Dish dish = new Dish();
               Menu menu = new Menu(AllDishs);         //将菜品信息放入菜单中
               Record record = new Record();
               Order order = new Order();
               dish.name = input.next();  //用户输入菜名
               int portion;   //份额
               while(dish.name != "end") {
                   if(menu.searchDish(dish.name) == false) {
                       System.out.println(dish.name + "does not exist");
                   }
                   else {
                       portion = input.nextInt();
                       record.setRecord(dish.name,portion);
                       order.setOnerecord(record.ADish, record.num);
                   }
                   dish.name = input.next();
               }
               System.out.print(order.getTotalprice());
           }
    }

class Dish{   //一道菜类
    String name;
    int price;
}

class Menu{   //菜单类
    ArrayList<Dish> alldishs = new ArrayList<Dish>();
    Menu(ArrayList<Dish> all){            //构造方法(带参构造)
        int i = 0;
        for(Dish d : all) {
            alldishs.get(i).name = d.name;
            alldishs.get(i).price = d.price;
            i++;
        }
    }
    void showMenu(){      //展示菜单界面
        for(int i = 0; i < alldishs.size();i++) {
            System.out.print(alldishs.get(i).name + " ");
            System.out.println(alldishs.get(i).price);
        }
    }
    boolean searchDish(String name1) {      //在菜单查找菜品
        for(Dish adish : alldishs) {
            if(adish.name.equals(name1)) 
            return true;
        }
        return false;
    }    
}

class Record{  //记录类
    Dish ADish = new Dish();     //记录菜名
    int num;    //记录份额
    void setRecord(String onedishname, int n) {   //写一条记录
        ADish.name = onedishname;
        num = n;
    }
    float getEachprice() {         //得到一条记录的价格
        return ADish.price*num;
    }
}

class Order{   //订单类
    ArrayList<Record> records = new ArrayList<Record>();
    static int n = 0;
    void setOnerecord(Dish dish, int num){      //将一条记录放入订单
        records.get(n).ADish = dish;
        records.get(n).num = num;
        n++;
    }
    float getTotalprice() {       //得到总价
        float sum = 0;
        for(int i = 0; i < records.size(); i++) {
            sum += records.get(i).getEachprice();
        }
        return sum;
    }
}

Help!QAQ

  • 写回答

1条回答 默认 最新

  • 小秋Kaito 2023-03-28 16:20
    关注

    按目前你的逻辑,第10到19行应该改成这样:

            for (int i=0; i < AllDishs_names.length; i++){ // 确保AllDishs_names数组与AllDishs_price数组的length一样
                Dish dish = new Dish();
                dish.name = AllDishs_names[i];
                dish.price = AllDishs_price[i];
                AllDishs.add(dish);
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 4月5日
  • 已采纳回答 3月28日
  • 创建了问题 3月27日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装