[英]Java : How do I use add in Arraylist?
我正在尝试使用 Arraylist 编写购物车程序。 我想做这个 --> 当用户输入 1 或添加项目时,它会添加用户想要添加的项目的(数量、名称、价格)。 但是,我在最后一部分遇到了错误,因为“购物车”无法解决。 我知道这是因为“ArrayList cart = new ArrayList(i);” 在 for 循环中,但我不知道如何解决这个问题。 另外,我是否正确使用 Arraylist ? 我已经挣扎了3天,仍然不确定..
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class item {
public item(int quantity, String name, double price) {
}
public static void main(String[] args)
{
item();
}//end main
public static void item()
{
Scanner myinput = new Scanner(System.in);
int total ;
int quantity;
String name = null;
double price;
String option;
System.out.println("How many kind of items do you have?");
total = myinput.nextInt();
for (int i = 1; i<=total; i++)
{
System.out.println("Please type the quantity of your item(s) (ex : If you have 2 shirts, please type 2)");
quantity = myinput.nextInt();
if (quantity == 0)
{
break;
}
System.out.println("Please type the name of your item(s)");
name = myinput.next();
System.out.println("Please type the price of your item(s)");
price = myinput.nextDouble();
ArrayList<item> cart = new ArrayList<item>(i);
}
System.out.println("Please choose one option below \n1) Add item(s) \n2) Remove item(s) \n3) View Cart \n4) Checkout \n5) Exit");
option = myinput.next();
switch(option)
{
case "1" , "Add item(s)":
System.out.print("Please type the name of your item(s)");
name = myinput.next();
System.out.print("Please type the price of your item(s)");
price = myinput.nextDouble();
System.out.print("Please type the quantity of your item(s)");
quantity = myinput.nextInt();
cart.add(new item(quantity, name, price)); //gets error in "cart"
System.out.println(Arrays.toString(cart)); //gets error in "cart"**strong text**
break;
}
}//end item
}//end class
在您的switch
语句中,您引用了一个名为cart
的变量。 但当时不存在这样的变量。
您拥有的唯一cart
是在您的for
循环中定义的。 所以每次通过循环,你都会实例化一个新的cart
(一个新的ArrayList
对象),然后立即丢弃它。 任何在循环内定义且从未在循环外共享的变量,都会立即成为垃圾收集的候选对象,变得无法访问,最终将被垃圾收集器删除。
在控制流到达您的switch
时,您的代码没有cart
object 可用。
将cart
列表的定义移到for
循环之外。
顺便说一句,遵循 Java 的命名约定(class 的初始大写,方法和变量的初始小写)和缩进约定将使您的代码更易于阅读和理解。
cart.add(new item(quantity, name, price)); //gets error in "cart"
这将无法访问,因为您已在本地 scope 中定义了它。
如果这符合您的要求,请查看以下内容:
import java.util.ArrayList;
import java.util.Scanner;
public class Item {
private int quantity;
private String name;
private double price;
public Item(){
}
public Item(int quantity, String name, double price) {
this.quantity = quantity;
this.name = name;
this.price = price;
}
public static void main(String[] args)
{
item();
}//end main
public static void item()
{
Scanner myinput = new Scanner(System.in);
int total ;
int quantity;
String name = null;
double price;
String option;
ArrayList<Item> cart = new ArrayList<Item>();
System.out.println("How many kind of items do you have?");
total = myinput.nextInt();
for (int i = 0; i<=total; i++)
{
System.out.println("Please type the quantity of your item(s) (ex : If you have 2 shirts, please type 2)");
Item item=new Item();
item.quantity = myinput.nextInt();
if (item.quantity== 0)
{
break;
}
System.out.println("Please type the name of your item(s)");
item.name = myinput.next();
System.out.println("Please type the price of your item(s)");
item.price = myinput.nextDouble();
cart.add(item);
//ArrayList<Item> cart = new ArrayList<Item>(i);
}
System.out.println("Please choose one option below \n1) Add item(s) \n2) Remove item(s) \n3) View Cart \n4) Checkout \n5) Exit");
option = myinput.next();
switch(option)
{
case "1" :
System.out.print("Please type the name of your item(s)");
name = myinput.next();
System.out.print("Please type the price of your item(s)");
price = myinput.nextDouble();
System.out.print("Please type the quantity of your item(s)");
quantity = myinput.nextInt();
cart.add(new Item(quantity, name, price)); //gets error in "cart"
for(int i=0;i<cart.size();i++){
System.out.println(cart.get(i));
}
break;
}
}//end item
@Override
public String toString() {
return "Item{" +
"quantity=" + quantity +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.