Java array declaration, creation, initialization
How to declare a one-dimensional array:
type var[]; or type[] var;
When declaring an array, you cannot specify its length (the number of elements in the array).
In Java, the keyword new is used to create an array object. The format is:
Array name = new array element type [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: array definition is performed separately from the operations of allocating space and assigning values to the array;
2. Static initialization: allocate space and assign values to array elements while defining numbers;
3. Default initialization: The array is a reference type, and its elements are equivalent to the member variables of the class. Therefore, after the array allocates space, each element is also initialized according to the rules of member variables.
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(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]) ; } } JAVA basics one-dimensional arrays and multi-dimensional arrays
In the Java language, array is the simplest composite data type. An array is a collection of ordered data. Each element in the array has the same data type. A unified array name and subscript can be used to uniquely identify the elements in the array. Arrays include one-dimensional arrays and multi-dimensional arrays.
1. Definition of one-dimensional array
type arrayName[];
Type (type) can be any data type in Java, including simple types and composite types.
For example:
int intArray[]; Date dateArray[];
2. Initialization of one-dimensional array
(1) Static initialization
int intArray[]={1,2,3,4}; String stringArray[]={"abc", "How", "you"};(2) Dynamic initialization
1) Simple type array
int intArray[]; intArray = new int[5];
2) Array of composite type
String stringArray[ ]; String stringArray = new String[3];/*Open up a reference space (32 bits) for each element in the array */ stringArray[0]= new String("How");//For the first one Create space for array elements stringArray[1]= new String("are");//Open space for the second array element stringArray[2]= new String("you");// Create space for the third array element3. Reference to one-dimensional array element
Array elements are referenced as:
arrayName[index]
Index is the array subscript, which can be an integer constant or an expression. The subscript starts from 0. Each array has an attribute length that specifies its length, for example: intArray.length specifies the length of the array intArray.
multidimensional array
In the Java language, multidimensional arrays are regarded as arrays of arrays.
1. Definition of two-dimensional array
type arrayName[ ][ ]; type [ ][ ]arrayName;
2. Initialization of two-dimensional array
(1) Static initialization
int intArray[ ][ ]={{1,2},{2,3},{3,4,5}};In the Java language, since a two-dimensional array is regarded as an array of arrays, the array space is not allocated continuously, so the size of each dimension of the two-dimensional array is not required to be the same.
(2) Dynamic initialization
1) Directly allocate space for each dimension in the following format:
arrayName = new type[arrayLength1][arrayLength2]; int a[ ][ ] = new int[2][3];
2) Starting from the highest dimension, allocate space to each dimension:
arrayName = new type[arrayLength1][ ]; arrayName[0] = new type[arrayLength20]; arrayName[1] = new type[arrayLength21]; … arrayName[arrayLength1-1] = new type[arrayLength2n];
3) Example: The dynamic initialization of a two-dimensional simple data type array is as follows,
int a[ ][ ] = new int[2][ ]; a[0] = new int[3]; a[1] = new int[5];
For arrays of two-dimensional composite data types, reference space must be allocated for the highest dimension first, and then space for the lower dimensions must be allocated sequentially. Furthermore, space must be allocated separately for each array element.
For example:
String s[ ][ ] = new String[2][ ]; s[0]= new String[2];//Allocate reference space for the highest dimension s[1]= new String[2]; //For the highest dimension Allocate reference space s[0][0]= new String("Good");// Allocate space separately for each array element s[0][1]= new String("Luck");// For each array element Array elements are allocated space individually s[1][0]= new String("to");// Allocate separate space for each array element s[1][1]= new String("You");// Allocate separate space for each array element3. Reference to two-dimensional array elements
For each element in the two-dimensional array, the reference method is: arrayName[index1][index2]
For example:
num[1][0];
4. Example of two-dimensional array:
[Example] Multiplying two matrices
public class MatrixMultiply{ public static void main(String args[]){ int i,j,k; int a[][]=new int [2][3]; //Dynamic initialization of a two-dimensional array int b[] []={{1,5,2,8},{5,9,10,-3},{2,7,-5,-18}};//Static initialization
a two-dimensional array
int c[][]=new int[2][4]; //Dynamic initialization of a two-dimensional array for (i=0;i<2;i++) for (j=0; j<3;j++) a[ j]=(i+1)*(j+2); for (i=0;i<2;i++){ for (j=0;j<4;j++){ c[j]=0; for( k=0;k<3;k++) c[j]+=a[k]*b[k][j]; } } System.out.println("*******Matrix C********");// Print Matrix C mark for(i=0;i<2;i++){ for (j=0;j<4;j++) System.out.println(c[j]+" "); System.out.println( ); } } }