2016/1/21 练习 arraylist 1,添加 add() 2,遍历集合

 1 package shuzu;
 2 
 3 public class Customer {
 4     //从源码中  使用字段生成构造函数
 5     public Customer(String good, int price) {
 6         super();
 7         this.good = good;
 8         this.price = price;
 9     }
10     private String good;
11     private int price;
12     //从源码中  使用  自动生成Get和Set方法
13     public String getGood() {
14         return good;
15     }
16     public void setGood(String good) {
17         this.good = good;
18     }
19     public int getPrice() {
20         return price;
21     }
22     public void setPrice(int price) {
23         this.price = price;
24     }
25      //从源码中 自动生成tostring方法
26     @Override
27     public String toString() { 
28         return "Customer [good=" + good + ", price=" + price + "]";
29     }
30     
31     }
 1 package shuzu;
 2 
 3 import java.util.ArrayList;
 4 
 5 public class Testlist {
 6 
 7     
 8     public static void main(String[] args) {
 9         ArrayList<Customer> cus=new ArrayList<Customer>();
10         cus.add(new Customer("衬衫",100));//匿名对象
11         cus.add(new Customer("西裤",200));//匿名对象
12         cus.add(new Customer("鞋子",300));//匿名对象
13         Customer cc=new Customer("帽子",50);//明名对象
14         cus.add(cc);
15         for (int i=0;i<cus.size();i++){
16             Customer c=cus.get(i);
17             System.out.println(""+c.getGood()+""+c.getPrice()+"元");//没有自动生成 tostring方法时  输出方式
18             //System.out.println(""+c+"元");//自动生成toString方法时  输出方式
19         }
20     }
21 
22 }

最后两条输出语句  输出结果相同   效果不同

转载于:https://www.cnblogs.com/haodayikeshu/p/5150039.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值