Walk-through exercises for the Java 15-3 Collection

Source: Internet
Author: User
Tags getcolor

Exercise: Store 5 animal objects in a collection and traverse the animal objects.
Analysis:
A: Creating Animal classes
A: Non-parametric construction method
B: A method of constructing a parameter
C:get, set method
B: Create a Collection Object
A:collection animal = new Arrarylist ();
C: Create Animal objects
B:5 species of animals, feel free to think
D: Add animal objects into the collection
C: Add into the animal
E: Convert set to Array
D:object[] ToArray (): Sets the set to the array, you can implement the collection of traversal
F: Traversing an array

Animal type:

1  PackageZl_objecttest1;2 3  Public classAnimal {4 5 PrivateString name;6 PrivateString Color;7 Private intAge ;8 9 Ten  PublicAnimal () { One Super(); A  - } -  the  -  -  -  PublicAnimal (string name, string color,intAge ) { + Super(); -  This. Name =name; +Color =color; A  This. Age =Age ; at } -  -   -  -  -  PublicString GetName () { in returnname; - } to  Public voidsetName (String name) { +  This. Name =name; - } the  PublicString GetColor () { * returnColor; $ }Panax Notoginseng  Public voidsetcolor (String color) { -Color =color; the } +  Public intGetage () { A returnAge ; the } +  Public voidSetage (intAge ) { -  This. Age =Age ; $ } $  -   -  the  -  PublicString toString () {Wuyi return"Animal [name=" + name + ", color=" + Color + ", age=" + Age the+ "]"; - } Wu}




Implementation class:

1  PackageZl_objecttest1;2 3 Importjava.util.ArrayList;4 Importjava.util.Collection;5 6 7  Public classanimalcollection {8 9  Public Static voidMain (string[] args) {Ten  One //To create a collection object ACollection animal =NewArrayList (); -  - //To create a collection object theAnimal A1 =NewAnimal ("Cat", "Tiger pattern", 2); -Animal A2 =NewAnimal ("Dutch Pig", "Pink", 1); -Animal A3 =NewAnimal ("Eagle", "Black and White", 4); -Animal A4 =NewAnimal ("Parrot", "Colorful", 2); +Animal A5 =NewAnimal ("Police dog", "Black", 3); -  + //Add animal objects to the collection A //Animal.add (A1); Here is an upward transformation of Object ABJ = "A1"; at Animal.add (A2); - Animal.add (A3); - Animal.add (A4); - Animal.add (A5); -  - //convert a collection to an array in //object[] ToArray (): Sets the set to the array, you can implement the collection of traversal -  toobject[] ABJ =Animal.toarray (); +  - //iterating through an array the  for(intx = 0; x < abj.length; X + +) { * //System.out.println (abj[x]);//This prints out the address value. $ Panax Notoginseng //to make a sub-downward transition, you can call the method inside the animal -Animal s =(Animal) abj[x]; theSystem.out.println (s);//This calls the animal inside the Tostrin () + //call the format you want to display yourself ASystem.out.println ("kind:" + s.getname () + "\ T color:" +S.getcolor () the+ "\ t Age:" +s.getage ()); + } -  $ } $  -}

Walk-through exercises for the Java 15-3 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.