我正在写一个学校的计划,作为收银机。我要求输入物品的价格并将它们播放到正在进行的ArrayList中,直到用户输入-1或0. 0是在错误的情况下重新输入先前的价格并且-1终止循环。
我得到了
java.lang.IndexOutOfBoundsException:索引0超出长度为0的范围
我尝试运行代码时出错。我必须包含一个名为removeLastEntry()
的方法,该方法将删除用户输入0时输入到数组中的最后一个价格。如何确保填充数组,我确实删除了最后一个条目?
我在Eclipse中运行Java 11。
代码在没有使用方法的情况下运行正常,因为我减少了我的计数器,并且在循环的下一次迭代中,先前的数组位置被覆盖,无论它是否已被删除。方法本身设置为删除ArrayList.size() - 1,以便删除最后一个条目。我用-2和0试过这个,它仍然用完了界限。
我读过以前的问题,很多人都没有填充数组。所以我运行了一个打印存根,以确保ArrayList已正确填充,并且它具有:当两个项目放入ArrayList大小时等于2.错误代码也会增加我放入代码的项目但是总是项目 - 1个项目超出范围的索引 - 1长度我确定我犯了一个菜鸟错误但是我找不到它而且它让我疯了!
对于完整错误上下文:
线程“main”中的异常java.lang.IndexOutOfBoundsException:java.base / jdk.internal中java.base / jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)的长度为0的索引0超出范围。 java.base / java.util.Objects.checkIndex(Objects.java:)中的java.base / jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)中的util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) 372)在C_M_iDeaProject.main的java.base / java.util.ArrayList.get(ArrayList.java:458)处(C_M_iDeaProject.java:76)
// declare our array list which will hold our prices!
ArrayList<Double> prices = new ArrayList<Double>();
// declaring variables to terminate loop, count prices, total prices, and current entry for conditions
int counter = 0;
double entry = 0;
double total = 0;
// our loop to continuously add prices to the array list from input
while (entry != -1) {
System.out.println("Enter a price for item #" + (counter+1) + ": ");
entry = myInput.nextDouble();
// if the entry is a price we will add it to prices and continue with the loop
if (entry != 0 && entry != -1) {
prices.add(entry);
total += entry;
counter++;
}
//if the entry is 0 we will revert back to the previous iteration to re-add
else if (entry == 0.0) {
total -= prices.get(counter-1);
removeLastEntry(prices);
counter--;
}
public static void removeLastEntry(ArrayList<Double> anArrayList) {
anArrayList.remove(anArrayList.size()-1);
}
你可以添加一张支票来查看if the list is empty:
// declare our array list which will hold our prices!
ArrayList<Double> prices = new ArrayList<Double>();
// declaring variables to terminate loop, count prices, total prices, and current entry for conditions
int counter = 0;
double entry = 0;
double total = 0;
// our loop to continuously add prices to the array list from input
while (entry != -1) {
System.out.println("Enter a price for item #" + (counter+1) + ": ");
entry = myInput.nextDouble();
// if the entry is a price we will add it to prices and continue with the loop
if (entry != 0 && entry != -1) {
prices.add(entry);
total += entry;
counter++;
}
//if the entry is 0 we will revert back to the previous iteration to re-add
else if (entry == 0.0) {
total -= prices.get(counter-1);
removeLastEntry(prices);
counter--;
}
public static void removeLastEntry(ArrayList<Double> anArrayList) {
if(!anArrayList.isEmpty()) {
anArrayList.remove(anArrayList.size()-1);
}
}
我们通过在尝试删除列表中的最后一个元素之前检查列表是否已经为空来解决问题 - 以防万一你收到的第一个值为零:)我们编辑了原始代码以封装有关约定的行为(-1退出, 0删除最后一个值)并避免每次我们需要检查时违反该原则。
List<Double> prices = new ArrayList<Double>();
// declaring variables to terminate loop, count prices, total prices, and current entry for conditions
int counter = 0;
double entry = 0;
double total = 0;
// our loop to continuously add prices to the array list from input
while (!isExit(entry)) {
System.out.println(String.format("Enter a price for item # %s: ", counter+1));
entry = myInput.nextDouble();
// if the entry is a price we will add it to prices and continue with the loop
if(isExit(entry)){
//TODO exit
}
if(isRemove(entry)){
if(!list.isEmpty()){
total -= removeLastEntry(prices);
counter--;
}
}else{
prices.add(entry);
total += entry;
counter++;
}
}
private boolean isExit(double value){
return value==-1;
}
private boolean isRemove(double entry){
return entry==0;
}
public static double removeLastEntry(List<Double> list) {
double last = list.get(list.size()-1);
list.remove(list.size()-1)
return last;
}