ArrayList

ArrayList创建集合添加对象并遍历

ArrayList<Product> array= new ArrayList<Product>();
Product s1 = new Product("1.遥控飞机", 168);	
	Product s2 = new Product("2.变形金刚", 999);
	Product s3 = new Product("3.芭比娃娃", 136);
	Product s4 = new Product("4.玩具手枪", 129);
	Product s5 = new Product("5.趣味拼图", 99);
	Product s6 = new Product("6.玩具积木", 100);
	public Shopping() {
		
	}
	
	public void shops(){//显示所有的商品
		array.add(s1);
		array.add(s2);
		array.add(s3);
		array.add(s4);
		array.add(s5);
		array.add(s6);
		System.out.println("-----------东京玩具汇总-----------");
		for (int x = 0; x < array.size(); x++) {
			Product s = (Product) array.get(x);
			System.out.println("\t"+s.getProduct() + "\t\t" + s.getPrice());
		}
		
	}

调对象:

Product s = (Product) array.get(x);
			System.out.println("\t"+s.getProduct() + "\t\t" + s.getPrice());

跨包调方法:
方法名 s = new 方法名();
s.方法名;
模拟购物车删除商品的代码:

do{
				System.out.println("请输入要删除的商品");
				int cproduct =sc.nextInt();
				if(count[cproduct-1]!=0){
				System.out.println("请输入要删除的数量");
				int ccount =sc.nextInt();
				reduce(cproduct, ccount);
				if(count[cproduct-1]==0){
					product[cproduct-1]=null;
				}else{
					System.out.println("成功删除"+ccount+"件"+product[cproduct-1]);
				}
				}else{
					System.out.println("该商品不在您的购物车");
				}
				System.out.println("是否继续删除:请输入y/n");
				cdelet=sc.next();
				
				}while(cdelet.equals("y"));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值