Basic tutorial on Java arrays

  • 2020-04-01 04:16:16
  • OfStack

Java array declaration, creation, initialization
Declaration of one-dimensional array:


type var[];  or type[] var;

You cannot declare an array by specifying its length (the number of elements in the array),

In Java, the keyword new is used to create an array object. The format is:


 An array of  = new  The type of the array element  [ The number of elements in an array ]

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 array definition is conducted 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: an array is a reference type whose elements correspond to the member variables of the class, so after the array is allocated space, each element is also initialized by the hermit according to the rules of the 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]) ;   
   }   
}  

One - dimensional arrays and multi - dimensional arrays
Arrays are one of the simplest composite data types in the Java language. An array is a collection of ordered data. Each element in an array has the same data type and can be uniquely determined by a uniform array name and index. Arrays have one-dimensional arrays and multidimensional arrays.

1. One dimensional array definition


type arrayName[ ] ;  

Types (types) can be any data type in Java, including simple types and composite types.

Such as:


int intArray[ ] ;   
Date dateArray[]; 

2. Initialization of a one-dimensional array

(1) static initialization


int intArray[]={1,2,3,4};  
String stringArray[]={"abc", "How", "you"}; 

(2) dynamic initialization

1) arrays of simple types


int intArray[];  
intArray = new 
int[5]; 

2) arrays of compound types


String stringArray[ ];  
String stringArray = new String[3]; 
stringArray[0]= new String("How");//Make room for the first array element
stringArray[1]= new String("are");//Make room for the second array element
stringArray[2]= new String("you");//Make room for the third array element

3. A reference to a one-dimensional array element

The reference method of array elements is:

ArrayName [index]
Index is the index of an array, which can be an integer constant or an expression, starting at 0. Each array has a property length to indicate its length, for example, intarray.length to indicate the length of an array.

Multidimensional array

In the Java language, multidimensional arrays are considered arrays of arrays.

1. The definition of a two-dimensional array


type arrayName[ ][ ] ;   
type [ ][ ]arrayName; 

2. Initialization of a two-dimensional array

(1) static initialization


int intArray[ ][ ]={{1,2},{2,3},{3,4,5}}; 

In the Java language, because two-dimensional arrays are treated as arrays of arrays, the array space is not allocated consecutively, so it is not required that two-dimensional arrays each have the same size.

(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 for 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 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, you must first allocate reference space for the highest dimension and then, in turn, for the lower dimension. Also, each array element must be allocated a separate space.

Such as:


String s[ ][ ] = new String[2][ ];  
s[0]= new String[2];//Allocate the reference space for the highest dimension
s[1]= new String[2]; //Allocate the reference space for the highest dimension
s[0][0]= new String("Good");//Allocate space for each array element separately
s[0][1]= new String("Luck");//Allocate space for each array element separately
s[1][0]= new String("to");//Allocate space for each array element separately
s[1][1]= new String("You");//Allocate space for each array element separately

3. A reference to a two-dimensional array element

For each element in a two-dimensional array, arrayName[index1][index2]

Such as:


num[1][0]; 

4. Example of two-dimensional array:

Multiply two matrices


public 
class MatrixMultiply{  
public 
static 
void main(String args[]){  
int i,j,k;  
int a[][]=new 
int [2][3]; //Initializes a two-dimensional array dynamically
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]; //Initializes a two-dimensional array dynamically
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 tags
for(i=0;i<2;i++){  
for (j=0;j<4;j++)  
System.out.println(c[j]+" ");  
System.out.println();  
}  
}  
} 


Related articles: