在列表中购买项目-线程中的例外;“主要”;java.lang.IndexOutOfBoundsException:索引:0,大小:0
我有两个在列表中购买项目-线程中的例外;“主要”;java.lang.IndexOutOfBoundsException:索引:0,大小:0,java,Java,我有两个列表和字符串 public static List<Items> pawnItemsList = new ArrayList<>(); public static List<Items> itemsList = new ArrayList<>(); public static String[][] itemsAttributes = new String[][] { {"Color TV ", "11
列表和字符串
public static List<Items> pawnItemsList = new ArrayList<>();
public static List<Items> itemsList = new ArrayList<>();
public static String[][] itemsAttributes = new String[][]
{
{"Color TV ", "113", },
{"Microwave ", "322",},
{"Computer ", "1564",},
{"Stereo ","402"}
};
我还有另一种方法:
if(pawnItemsList.size() == 0)
{
System.out.println("You don't have any items");
pawnShopMenu();
}
else
{
printPawnItemsList();
System.out.println("You want to sell something? 1-Yes 0-No");
int pick = in.nextInt();
in.nextLine();
if (pick == 1) {
printPawnItemsList();
System.out.println("\nWhich item do you want to sell?(type the index)");
int choice = in.nextInt();
in.nextLine();
pawnItemsList.remove(choice);
itemsList.add(new Items(itemsAttributes[choice - 1][0], Integer.parseInt(itemsAttributes[choice - 1][1])));
budget.money = budget.money + pawnItemsList.get(choice - 1).getPrice();
System.out.println("You sold the " + pawnItemsList.get(choice - 1).getName());
System.out.println("BUDGET=" + budget.money);
}
if (pick == 0)
{
pawnShopMenu();
}
当我想用第三种方法销售产品时,我在线程“main”java.lang.IndexOutOfBoundsException中得到了异常。我是初学者,所以我只知道基本的方法。当我购买物品时,我想将其从itemsList
中删除,并将其添加到pawnItemsList
。当我销售产品时,我想将它们从pawnItemsList
中删除,并添加到itemsList
中,但它不起作用。如果您试图从列表或数组中删除某个内容,但该内容不存在,则会出现java.lang.IndexOutOfBoundsException
。
在从列表中删除任何内容之前,请检查列表大小是否大于零
例如:
if (pick == 1) {
printPawnItemsList();
System.out.println("\nWhich item do you want to sell?(type the index)");
int choice = in.nextInt();
in.nextLine();
//check if pawnItemsList size >0
if(pawnItemsList.size()>0)
pawnItemsList.remove(choice);
itemsList.add(new Items(itemsAttributes[choice - 1][0], Integer.parseInt(itemsAttributes[choice - 1][1])));
budget.money = budget.money + pawnItemsList.get(choice - 1).getPrice();
System.out.println("You sold the " + pawnItemsList.get(choice - 1).getName());
System.out.println("BUDGET=" + budget.money);
}
如果试图从列表或数组中删除某个内容,但该内容不存在,则会得到java.lang.IndexOutOfBoundsException
。
在从列表中删除任何内容之前,请检查列表大小是否大于零
例如:
if (pick == 1) {
printPawnItemsList();
System.out.println("\nWhich item do you want to sell?(type the index)");
int choice = in.nextInt();
in.nextLine();
//check if pawnItemsList size >0
if(pawnItemsList.size()>0)
pawnItemsList.remove(choice);
itemsList.add(new Items(itemsAttributes[choice - 1][0], Integer.parseInt(itemsAttributes[choice - 1][1])));
budget.money = budget.money + pawnItemsList.get(choice - 1).getPrice();
System.out.println("You sold the " + pawnItemsList.get(choice - 1).getName());
System.out.println("BUDGET=" + budget.money);
}
您的pawnItemList.get(choize-i)
不保证有条目,事实上您跳过了购买,它将是空的。
如果选择1,则会出现该错误。(对于入口为0的列表,索引0处没有任何内容,也称为新闻列表();)
这不一定是问题所在,因为我不知道您是如何安排代码的。我假设您可以跳过/重复购买过程 您的pawnItemList.get(choize-i)
不保证有条目,事实上您跳过了购买,它将是空的。
如果选择1,则会出现该错误。(对于入口为0的列表,索引0处没有任何内容,也称为新闻列表();)
这不一定是问题所在,因为我不知道您是如何安排代码的。我假设您可以跳过/重复购买过程 你好,阿德丽娜·乔治亚娜,欢迎来到斯塔克沃夫。我认为如果你在问题的第一段陈述你想做什么,然后在最后一段重复,你的问题会更清楚。另外,一个完整的stacktrace也会非常有用,这是一个运行时错误,而不是编译错误,顺便说一句,你好,Adelina Georgiana,欢迎使用StackOverflow。我认为如果你在问题的第一段陈述你想做什么,然后在最后一段重复,你的问题会更清楚。另外,一个完整的stacktrace将非常有用,顺便说一句,这是一个运行时错误,而不是编译错误