Java Array Declaration, initialization

Source: Internet
Author: User
Tags array definition

How to declare a dimension array:
Type var[]; or type[] var;

When declaring an array, you cannot specify its length (the number of elements in the array).

In Java, you use the keyword new to create an array object in the following format:
Array name = new array element type [number of array elements]

Instance:
Testnew.java:

Program code:

  1. Public class Testnew
  2. {
  3. public static void Main (String args[]) {
  4. int[] s;
  5. int i;
  6. s = new int[5];
  7. For (i = 0; i < 5; i++) {
  8. S[i] = i;
  9. }
  10. For (i = 4; I >= 0; i--) {
  11. System.out.println ("" + s[i]);
  12. }
  13. }
  14. }


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

Java Array Declaration, initialization

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.