【问题标题】:What does Index: -1 say? java.lang.IndexOutOfBoundsException: Index: -1, Size: 9 [duplicate]指数:-1 说什么? java.lang.IndexOutOfBoundsException:索引:-1,大小:9 [重复]
【发布时间】:2017-04-28 11:15:01
【问题描述】:

我的代码崩溃并显示以下消息: java.lang.IndexOutOfBoundsException:索引:-1,大小:9

索引 -1 是什么意思?

【问题讨论】:

  • index :你尝试访问的索引size的实际大小。
  • 索引 -1 表示您将 -1 作为索引传递给数组。您如何显示导致此异常的代码?

标签: java indexoutofboundsexception


【解决方案1】:

在您的代码中的某处,您的索引变量正在获得负值。能否提供一些代码?

【讨论】:

  • 它在“Vehicle vehicle = chanceMarket.get(model).remove(index);”代码崩溃。 public static Vehicle buyOccasionCar(String model, int index){d_occasionSales++;Vehicle vehicle = occasionMarket.get(model).remove(index);vehicle.resetPurchaseTick();registered_vehicles.put(vehicle.getModel(),registered_vehicles.get(vehicle.getModel())+1);return vehicle;}
  • 在对数组或列表进行任何操作之前,您应该检查索引是否具有正确的值。所以在执行之前:occasionMarket.get(model).remove(index); 你应该检查 if 'index >= 0 && index
  • 谢谢你的回答,我真的很陌生!如何查看索引?
【解决方案2】:

这意味着您使用的数组/列表等的总大小为 9 项。但是您正在尝试获取索引为“-1”的记录/项目。因为在大多数语言中,数组/列表索引从“0”到“size - 1”开始,所以不能使用小于 0 或大于数组/列表大小的索引

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2020-11-29
    相关资源
    最近更新 更多