Java Object array

Source: Internet
Author: User
Tags array definition

An array is a reference type, and a class is also a reference type, so if an array of objects represents a reference type, the other reference types are nested inside.

In the previous two articles are data of the basic data type, but the reference data type of the index can also define an array, such an array is called an array of objects.

Define an array of objects as an example of a class:

    • Dynamic initialization://The contents of an object array after opening are default values, i.e. null values
      • Class name Object array name [] = new class name [length];
      • Step through:
        • Declaring an Object array: Class name object array name [] = null;
        • Open Object array: Object array name = new class name [length];
    • Static initialization:
      • Class name Object array name [] =new class name []{instantiate object, instantiate object, instantiate object ...}

Example:

To dynamically generate an array of objects:

1 classbook_demo_1{2     PrivateString name;3     Private DoubleMoney ;4 5 6      Publicbook_demo_1 () {7     }8      PublicBook_demo_1 (String N,Doublem) {9Name =N;TenMoney =m; One     } A  -      PublicString GetName () { -         returnname; the     } -      Public DoubleGetmoney () { -         returnMoney ; -     } +  -      PublicString GetInfo () { +         return"Title:" + name + "\ T" + "price" +Money ; A     } at  -      Public voidSetName (String N) { -Name =N; -     } -      Public voidSetmoney (intm) { -Money =m; in     } - } to  +  Public classObject Array { -      Public Static voidMain (string[] args) { theBook_demo_1 Book [] =NewBOOK_DEMO_1[3];//open an array of 3-length objects *Book[0] =NewBook_demo_1 ("Java development", 66.6); $BOOK[1] =NewBook_demo_1 ("Python", 99.9);Panax NotoginsengBOOK[2] =NewBook_demo_1 ("PHP", 11.1); -          for(inti = 0; i < book.length; i++) { the System.out.println (Book[i].getinfo ()); +         } A     } the}

Static initialization:

1 classbook_demo_1{2     PrivateString name;3     Private DoubleMoney ;4 5 6      Publicbook_demo_1 () {7     }8      PublicBook_demo_1 (String N,Doublem) {9Name =N;TenMoney =m; One     } A  -      PublicString GetName () { -         returnname; the     } -      Public DoubleGetmoney () { -         returnMoney ; -     } +  -      PublicString GetInfo () { +         return"Title:" + name + "\ T" + "price" +Money ; A     } at  -      Public voidSetName (String N) { -Name =N; -     } -      Public voidSetmoney (intm) { -Money =m; in     } - } to  +  Public classObject Array { -      Public Static voidMain (string[] args) { theBook_demo_1 Book [] =Newbook_demo_1[]{ *                 NewBook_demo_1 ("Java development", 66.6), $                 NewBook_demo_1 ("Python", 99.9),Panax Notoginseng                 NewBook_demo_1 ("PHP", 11.1), -         }; the          for(inti = 0; i < book.length; i++) { + System.out.println (Book[i].getinfo ()); A         } the     } +}

    • An array of objects is actually just handing over multiple objects to the array for unified management.
    • Object array definition syntax, object array is actually equal to multiple objects

Java 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.