zoukankan      html  css  js  c++  java
  • 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 }

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

  • 相关阅读:
    MarkDown 语言简单使用
    开机自启宽带连网设置问题
    JqueryEasyUI EasyLoader
    JqueryEasyUI $.Parser
    FileUpload一键自动上传
    Git读档
    JavaScript typeof运算符和数据类型
    JavaScript 函数 (function)
    Javascript 对象(Object)
    JavaScript 数组(Array)
  • 原文地址:https://www.cnblogs.com/haodayikeshu/p/5150039.html
Copyright © 2011-2022 走看看