This article describes several related aspects of the Java array, tells the statement, creation and initialization of the Java array, and gives the corresponding code.
How to declare the one -dimensional array : type var []; or Type [] var;
When the array is declared, it cannot specify its length (the number of elements in the array), and
Use keywords New to create an array object in Java. The format is : the type of array name = NEW array element [Number of array elements]
Example: 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]);}}}initialization:
1. Dynamic initialization: The operation of the definition of the array is separated from the operation of the number of assignments and assignments;
2. Static initialization: While defining numbers, it will allocate space and assign a value for array elements;
3. By default initialization: The array is a reference type. Its elements are equivalent to class member variables. Therefore, after the number of arrays allocate space, each element is initialized by the hermit according to the rules of the member variable.
Example:
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 (new date (new date ( 2008,4,4);}} class date {int Year, month, day; date (int Year, int Month, int days) {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 = host; this.min = min = min = min = min = min = min = min = min = min = min = min = min = min = min = min = min = min = min = 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 an example of the Java array statement, creation, and initialization. I hope everyone can understand and help everyone's learning.