(go) deep learning of Java two-dimensional arrays (static vs. dynamic)

Source: Internet
Author: User
Tags array definition

Turn from: http://developer.51cto.com/art/200906/128274.htm, thank you

Initialization

1. Dynamic initialization: The array definition is performed separately from the assignment of the space and assignment to a set of values;
2. Static initialization: Allocates space and assigns values to the array elements while defining the numbers;
3. Default initialization: An array is a reference type and its elements are equivalent to the member variables of the class, so after the array allocates space, each element is also initialized by the hermit according to the rules of the member variable.
Instance:


Testd.java (Dynamic):

Program code:

  1. Public class TestD
  2. {
  3. public static void Main (String args[]) {
  4. int a[];
  5. A = new int[3];
  6. a[0] = 0;
  7. a[1] = 1;
  8. a[2] = 2;
  9. Date days[];
  10. Days = new date[3];
  11. days[0] = new Date (4,5);
  12. days[1] = new Date (2,31);
  13. days[2] = new Date (4,4);
  14. }
  15. }
  16. Class Date
  17. {
  18. int year,month,day;
  19. Date (int year,int. month,int day) {
  20. this.year = year;
  21. this.month = month;
  22. this.day = day;
  23. }
  24. }


Tests.java (Static):

Program code:

  1. Public class TestS
  2. {
  3. public static void Main (String args[]) {
  4. int a[] = {0,1,2};
  5. Time times [] = {New Time (1, +), New Time (5,3,      2)};
  6. }
  7. }
  8. Class time
  9. {
  10. int hour,min,sec;
  11. Time (int hour,int min,int sec) {
  12. this.hour = hour;
  13. this.min = min;
  14. this.sec = sec;
  15. }
  16. }


Testdefault.java (default):

Program code:

    1. Public class Testdefault
    2. {
    3. public static void Main (String args[]) {
    4. int a [] = new int [5];
    5. System.out.println ("" + a[3]);
    6. }
    7. }

(go) deep learning of Java two-dimensional arrays (static vs. dynamic)

Related Article

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.