(); public static List item-6ren">
gpt4 book ai didi

java - 购买列表中的项目 - 线程中出现异常 "main"java.lang.IndexOutOfBoundsException : Index: 0, 大小:0

转载 作者:太空宇宙 更新时间:2023-11-04 10:57:48 25 4
gpt4 key购买 nike

我有这 2 个 ListString

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"}
};

我有这种方法,可以从 itemsList 购买产品。

System.out.println("\nWhich item do you want to buy?(type the index)\n");
choice = in.nextInt();
in.nextLine();

if (choice == 1)
{
if(budget.money < tvPrice)
{
System.out.println("\nYou don't have enough money");
System.out.println("You have only: $" + budget.money);

}
else
{
budget.money = budget.money - tvPrice;
System.out.println("\nYour budget now:"+ budget.money);
itemsList.remove(0);
pawnItemsList.add(new Items(itemsAttributes[0][0], Integer.parseInt(itemsAttributes[0][1])));
System.out.println("\nYou bought a color TV\n");
}
}
if (choice == 2)
{
if(budget.money < microwavePrice)
{
System.out.println("\nYou don't have enough money");
System.out.println("You have only: $" + budget.money);
}
else {
budget.money = budget.money - microwavePrice;
System.out.println("\n Your budget now:" + budget.money);
itemsList.remove(1);
pawnItemsList.add(new Items(itemsAttributes[1][0], Integer.parseInt(itemsAttributes[1][1])));
System.out.println("\nYou bought a color microwave");
}
}
if (choice == 3)
{
if(budget.money < computerPrice)
{
System.out.println("\nYou don't have enough money");
System.out.println("You have only: $" + budget.money);

}
else {
budget.money = budget.money - computerPrice;
System.out.println("\nYour budget now:" + budget.money);
itemsList.remove(2);
pawnItemsList.add(new Items(itemsAttributes[2][0], Integer.parseInt(itemsAttributes[2][1])));
System.out.println("\nYou bought a computer");
}
}
if (choice == 4)
{
if(budget.money < stereoPrice)
{
System.out.println("\nYou don't have enough money");
System.out.println("You have only: $" + budget.money);

}
else {
budget.money = budget.money - stereoPrice;
System.out.println("\nYour budget now:" + budget.money);
itemsList.remove(3);
pawnItemsList.add(new Items(itemsAttributes[3][0], Integer.parseInt(itemsAttributes[3][1])));
System.out.println("\nYou bought a stereo");
}
}
System.out.println("If you want to buy something else press 1 and if you want to go to the Pawn press 2");
choice = in.nextInt();
in.nextLine();
if(choice == 1)
{
printItemsList();
buyItems();
}
if(choice == 2)
{
pawnItems();
}

我还有另一种方法:

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 - 购买列表中的项目 - 线程中出现异常 "main"java.lang.IndexOutOfBoundsException : Index: 0, 大小:0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47202611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com