Arrays

Arrays

An array is used to store more variables into 1 variable. It starts from int[] with square brackets +variable name and the definition of the datatype(in this case: new int[length]

Example:

 

int[] vector1D = new int[5]; // create 1D Array with the size of 5
int[][] vector2D = new int[5][5]  // create 2D Array with the size of 5   
int[] arr = {3, 4, 5, 2, 1}; // define attributes directly

Array declaration of each value 

vector1D[0] = 1;
vector2D[1] = 3;
vector2D[2] = 4;
vector2D[3] = 5;
…......


Iteration and Initialisation of 1D Array

for(int j = 0;j<5;j++) {  
         vector1D[j] = 0; 
         System.out.println(vector1D[j]);
     }

2D Array

 for(int i= 0;i<5;i++) {
         for(int j = 0;j<5;j++) {
             vector2D[i][j]= 1;
     } }
 

Leave a Reply

Your email address will not be published. Required fields are marked *