Java array declaration, creation, and initialization

Source: Internet
Author: User
Tags array definition

One-dimensional array declaration method:
Type var []; or type [] var;

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

In Java, use the keyword new to create an array object in the 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 separated from the operations for allocating space and assigning values to arrays;
2. Static initialization: when defining numbers, allocate space for array elements and assign values;
3. Default initialization: The array is a reference type, and its elements are equivalent to member variables of the class. Therefore, after the array is allocated space, each element is also initialized by the hermit according to the rules of the member variables.
Instance:

Testd. Java (dynamic ):

Program code:

  1. Public ClassTestd
  2. {
  3. Public Static VoidMain (string ARGs []) {
  4. IntA [];
  5. A =New Int[3];
  6. A [0] = 0;
  7. A [1] = 1;
  8. A [2] = 2;
  9. Date days [];
  10. Days =NewDate [3];
  11. Days [0] =NewDate (, 5 );
  12. Days [1] =NewDate (, 31 );
  13. Days [2] =NewDate (, 4 );
  14. }
  15. }
  16.  
  17. ClassDate
  18. {
  19. IntYear, month, day;
  20. Date (IntYear,IntMonth,IntDay ){
  21. This. Year = year;
  22. This. Month = month;
  23. This. Day = Day;
  24. }
  25. }
  26.  

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 (, 42), new time (, 54), new time (, 2 )};
  6. }
  7. }
  8.  
  9. Class time
  10. {
  11. Int hour, Min, SEC;
  12. Time (INT hour, int min, int Sec ){
  13. This. Hour = hour;
  14. This. min = min;
  15. This. sec = sec;
  16. }
  17. }

Testdefault. Java (default ):

Program code:

  1. Public ClassTestdefault
  2. {
  3. Public Static VoidMain (string ARGs []) {
  4. IntA [] =New Int[5];
  5. System. Out. println ("" + A [3]);
  6. }
  7. }

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.