2016/1/21 practice ArrayList 1, adding Add () 2, traversing the collection

Source: Internet
Author: User

1  PackageShuzu;2 3  Public classCustomer {4     //generate constructors from the source using fields5      PublicCustomer (String Good,intPrice ) {6         Super();7          This. Good =good;8          This. Price =Price ;9     }Ten     PrivateString good; One     Private intPrice ; A     //Use automatic generation of Get and set methods from source -      PublicString Getgood () { -         returngood; the     } -      Public voidSetgood (String good) { -          This. Good =good; -     } +      Public intGetPrice () { -         returnPrice ; +     } A      Public voidSetprice (intPrice ) { at          This. Price =Price ; -     } -      //automatically generate the ToString method from the source code - @Override -      PublicString toString () { -         return"Customer [good=" + Good + ", price=" + Price + "]"; in     } -      to}
1  PackageShuzu;2 3 Importjava.util.ArrayList;4 5  Public classtestlist {6 7     8      Public Static voidMain (string[] args) {9Arraylist<customer> cus=NewArraylist<customer>();TenCus.add (NewCustomer ("Shirt", 100)); //Anonymous object  OneCus.add (NewCustomer ("trousers", 200)); //Anonymous object  ACus.add (NewCustomer ("Shoes", 300)); //Anonymous object  -Customer cc=NewCustomer ("Hat", 50); //Ming name Object  - Cus.add (cc); the          for(intI=0;i<cus.size (); i++){ -Customer c=Cus.get (i); -System.out.println ("" +c.getgood () + "" +c.getprice () + "meta"); //No Output mode when the ToString method is generated automatically  -             //System.out.println ("+c+");//Output mode when the ToString method is generated automatically +         } -     } +  A}

The last two output statements output the same effect differently

2016/1/21 practice ArrayList 1, adding Add () 2, traversing the collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.