Arrays: Introduction & Explanation



  • An array is a collection of similar data elements stored at contiguous memory locations
  • It is the simplest data structure where each data element can be accessed directly by only using its index number
  • Index of the array starts from 0
  • Let us assume we want to store marks of students for all subjects. For storing the marks in each subject
    there is no need to define variables for each subject, rather we will use an array to store marks in each subject
  • marks[5] defines the array of the marks scored in the fifth subject. marks[0] is the marks stored
    in the first subject.marks[1] is the marks stored in the second subject and so on
 
Size of arrays
  1. The array has fixed size once the size is given to it it cannot be changed
  2. You can find the size of an array in java by querying an array's length property
  3. The array size is set permanently when the array is initialized
Code to find the size of array
            
                int [] demoarray = {1, 2, 3, 4, 5}; 
                int demoarraysize = demoarrray.length; 
                System.out.println("The array size is : " + demoarraysize);
            
        
Output

The array size is : 5

 
How to traverse an array
  1. Traversing an array means going through each element of the array one by one
  2. We can do this using loops
Code to traverse an array
            
                int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; 
                for(int i = 0; i < arr.length(); i++){ 
                    System.out.print(arr[i]); 
                }            
            
        
Output

1 2 3 4 5 6 7 8

 
How to access and print elements of an array
  1. We can access array elements using array index. Index are like array element address that will point to the element location
  2. Array indices start with 0. Meaning the first element in the array can be accessed as array[0], the second element as array[1] and so on
Code to access 4th element in the array
            
                int arr[] = {11, 22, 33, 44, 55, 66, 77, 88}; 
                System.out.println(arr[3]);            
            
        
Output

44

 
Code to access last element in the array
            
                int arr[] = {11, 22, 33, 44, 55, 66, 77, 88}; 
                System.out.println(arr[arr.length-1]);            
            
        
Output:

88

 
Sample Array Program
 
Java Code
                
    class Arrays{

        public int getMin(int[] array){
            int min = Integer.MAX_VALUE;
    
            for(int i = 0; i < array.length; i++){
                if(array[i] < min){
                    min = array[i];
                }
            }
    
            return min;
        }
    
    
        public int getMax(int[] array){
            int max = Integer.MIN_VALUE;
    
            for(int i = 0; i < array.length; i++){
                if(array[i] > max){
                    max = array[i];
                }
            }
    
            return max;
    
        }
        public static void main(String[] args){
    
            Arrays a = new Arrays();
            int[] rollNos = {21, 61, 51, 31, 71, 11, 41};
    
            System.out.println("The no of elements in the array is: " + rollNos.length);
            System.out.println("The minimum value from the array is: " + a.getMin(rollNos));
            System.out.println("The maximum value from the array is: " + a.getMax(rollNos));
        }
    }
                
              
Output

The no of elements in the array is: 7
The minimum value from the array is: 11
The maximum value from the array is: 71



Thanks for feedback.