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