我得到出界错误,而从数组列表中删除,即使数组列表的大小是正确的

  • 本文关键字:数组 列表 删除 出界 错误 java
  • 更新时间 :
  • 英文 :


这是我给数组列表添加数据的方式

System.out.println("please select part/s (-1 for return to the main menu)");
//array to store multiple inputs
String[] part = scanner.nextLine().split(",");
//adding customer computer parts to its order
for (int k = 0; k < part.length; k++) {
if (Integer.parseInt(part[k]) == -1) {
//thats just to break the loop but its not creating any problem
bool = false;
} else {
os.ord.addComputerPart(os.computerParts.get(Integer.parseInt(part[k])));
}
}

这里是添加输入的方法数组列表

public void addComputerPart(ComputerPart computerpart ){
parts.add(computerpart);
totalPrice += computerpart.getPrice();
}

下面是从数组列表

中删除数据的方法
//os is the class object and ord is the order class object which is the seperate class and order class contains the parts arraylist 
System.out.println( os.ord.getParts().size());
//printing the information about the customer and the order
System.out.println("here is the summary of your current order");
System.out.println(os.ord);
Scanner scxx = new Scanner(System.in);
//using the array to take more than one inputs
System.out.println("Please select part/s to be removed from the order (-1 for return to the main menu)");
String[] part = scxx.nextLine().split(",");
//deleting customer computer parts from  its order                                    
for (int k = 0; k < part.length; k++) {
if (Integer.parseInt(part[k]) == -1) {
delete = false;
} else {
os.ord.removeComputerPart(Integer.parseInt(part[k]));
}
}

在order类中我有一个从arrayList中移除数据的方法也就是移除数据

public void removeComputerPart(int index){
parts.remove(index);
totalPrice-= parts.get(index).getPrice();
}

现在的问题是,当我从数组列表中删除元素时它会给我indexoutfbound错误

4 --> as u can see that while printing the size of the arraylist i am getting 4 elements but when i delete the 4th element it gives me error 
here is the summary of your current order
(0): Product ID : INTCPU94496  Brand : INT  Model : 9700K  Price : 462.0  Core : i7
Socket : Intel
not compatible with : 
(1): Product ID : INTCPU12673  Brand : INT  Model : 9700F  Price : 396.0  Core : i7
Socket : Intel
not compatible with : 
(2): Product ID : INTCPU72675  Brand : INT  Model : 9900K  Price : 591.0  Core : i9
Socket : Intel
not compatible with : 
(3): Product ID : AMDCPU54823  Brand : AMD  Model : Ryzen 2200  Price : 200.0  Core : 4
Socket : AMD
not compatible with : 
Order Id : O104181 Total Price : $1649.0
Member customer discount : $82.45 total price after discount : $1566.55
Please select part/s to be removed from the order (-1 for return to the main menu)
3
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)

InsideremoveComputerPart()method首先扣除零件的价格,然后将该零件从列表中删除。removeComputerPart ()方法如下-

public void removeComputerPart(int index){
totalPrice-= parts.get(index).getPrice();   // first line
parts.remove(index);                        // second line
}

最新更新