Java Array declaration, creation, initialization detailed _java

Source: Internet
Author: User
Tags array definition

How to declare a one-dimensional array:

Type var[]; or type[] var;

You cannot specify its length (the number of elements in an array) when declaring an array.

Use the keyword New in Java to create an array object in the following format:

Array name = new array element type [number of array elements]

Instance:
Testnew.java:

Program code:

public class Testnew 
{public 
   static void Main (String args[]) { 
     int[] s; 
     int i; 
     s = new int[5]; 
     for (i = 0; i < 5; i++) { 
       s[i] = i; 
     } 
     for (i = 4; I >= 0; i--) { 
       System.out.println ("" + S[i]);}}  
 

Class
1. Dynamic initialization: The array definition is separated from the operation of the Arrays allocation space and assignment;
2. Static initialization: Assigning space and assigning values to array elements while defining numbers;
3. Default initialization: An array is a reference type and its elements are equivalent to a class's member variable, so after the array allocates space, each element is also initialized by the hermit according to the rule of the member variable.

Instance:

Testd.java (Dynamic):
Program code:

public class TESTD 
{public 
   static void Main (String args[]) { 
     int a[]; 
     A = new int[3]; 
     A[0] = 0; 
     A[1] = 1; 
     A[2] = 2; 
     Date days[]; 
     days = new Date[3]; 
     Days[0] = new Date (2008,4,5); 
     DAYS[1] = new Date (2008,2,31); 
     DAYS[2] = new Date (2008,4,4); 
   } 
 
Class Date 
{ 
   int year,month,day; 
   Date (int year, int month, int day) { 
     this.year = year; 
     This.month = month; 
     This.day = day; 
   } 
 
 

Tests.java (Static):
Program code:

public class TestS   
{public   
   static void Main (String args[]) {   
     int a[] = {0,1,2};   
     Time times [] = {new Time (19,42,42), New Time (1,23,54), New Time (5,3,2)}}   
 
Class time   
{   
   int hour,min,sec;   
   Time (int hour, int min, int sec) {   
     this.hour = hour;   
     this.min = min;   
     This.sec = sec;   
   }   
}  

Testdefault.java (default):
Program code:

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



The above is the Java array declaration, creation, initialization and other related knowledge, hope to help you, thank you for your support to this site!

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.