Java object-oriented object array

Source: Internet
Author: User

Today I learned an array of objects, write something to summarize. Not much nonsense, what is an object array?

The concept of an object array is that the object array is the object of the class, where each element of the array is assigned, the object is defined, and then the object is assigned directly to the array.

For example, using object arrays to manage multiple hero

1 Importjava.util.Arrays;2 ImportJava.util.Scanner;3 4  Public classTest4 {5 6     /**7      * @paramargs8      */9      Public Static voidMain (string[] args) {Ten         //TODO auto-generated Method Stub OneHeromanager hm=NewHeromanager (); ASYSTEM.OUT.PRINTLN ("------Add hero------"); - heros.print1 (); -Hm.add (NewHeros ("Guan Yu", "Male", 30)); theHm.add (NewHeros ("Zhang Fei", "Male", 31)); -Hm.add (NewHeros ("Liu Bei", "male", 32)); -Hm.add (NewHeros ("Caocao", "male", 33)); -Hm.add (NewHeros ("Sun Quan", "male", 34)); + hm.list (); -SYSTEM.OUT.PRINTLN ("------Find heroes------"); +Heros Q=hm.find (34); A q.print (); atSYSTEM.OUT.PRINTLN ("------Remove heroes------"); -Hm.delete (34); - hm.list (); -SYSTEM.OUT.PRINTLN ("------Update hero------"); -Hm.update (NewHeros ("Shian", "female", 31)); - hm.list (); in     } -  to } + //Management class - classheromanager{ the     PrivateHeros[] Heros=NewHeros[3]; *     Private intCount=0; $      Public voidAdd (Heros h) {Panax Notoginseng         if(count>=heros.length) { -             intnewlen=heros.length*3/2+1;//generally expands by half the original length theheros=arrays.copyof (heros, Newlen); +         } Aheros[count]=h; thecount++; +         } -      PublicHeros Find (intID) { $          for(inti=0;i<count;i++){ $             if(Heros[i].getid () = =ID) { -                 returnHeros[i]; -             } the         } -         return NULL;Wuyi          the     } -     //Guan Yu, Zhang bei Cao Wu      Public voidDeleteintID) {      -          for(inti=0;i<count;i++){ About             if(Heros[i].getid () = =ID) { $                  for(intj=i;j<count-1;j++){ -Heros[j]=heros[j+1]; -                 } -heros[count-1]=NULL; Acount--; +                  Break; the             } -         } $     } the      Public voidUpdate (Heros H1) { theHeros m=Find (H1.getid ()); the         if(m!=NULL){ the M.setname (H1.getname ()); - M.setsex (H1.getsex ()); in         } the     } the      Public voidlist () { About          the          for(inti=0;i<count;i++){ the heros[i].print (); the         } +     } -      the     Bayi     } the  the  - //Hero Class - classheros{ the     PrivateString name; the     PrivateString sex; the     Private intID; the      PublicHeros () { -          the     } the      PublicHeros (String name,string sex,intID) { the          This. name=name;94          This. sex=sex; the          This. id=ID; the     } the      PublicString GetName () {98         returnname; About     } - 101      Public voidsetName (String name) {102          This. Name =name;103     }104  the      PublicString Getsex () {106         returnsex;107     }108 109      Public voidsetsex (String sex) { the          This. Sex =sex;111     } the 113      Public intgetId () { the         returnID; the     } the 117      Public voidSetId (intID) {118          This. ID =ID;119     } - 121      Public voidprint () {122System.out.println (name+ "\ t" +sex+ "\ T" +ID);123     }124      Public Static voidprint1 () { theSystem.out.println ("name" + "\ T" + "gender" + "\ T" + "rank" + "\ T"));126     }127      -}

Java object-oriented object array

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.