Easy Tutorial
For Competitive Exams

Java Programming Array Small Programs More Programs

Array programs

Example 1:

Find Smallest Number in an Array:

class SmallestInArray{  
public static int smallest(int[] a, int total){  
int temp;  
for (int i = 0; i < total; i++)   
        {  
            for (int j = i + 1; j < total; j++)   
            {  
                if (a[i] > a[j])   
                {  
                    temp = a[i];  
                    a[i] = a[j];  
                    a[j] = temp;  
                }  
            }  
        }  
       return a[0];  
}  
public static void main(String args[]){  
int a[]={1,2,5,6,3,2};   
System.out.println("Smallest: "+smallest(a,6));  
}}  
Try it Yourself

Example 2:

Find the 2nd largest number in an array:

class SecondLargest{  
public static int secondLargest(int[] a, int total){  
int temp;  
for (int i = 0; i < total; i++)   
        {  
            for (int j = i + 1; j < total; j++)   
            {  
                if (a[i] > a[j])   
                {  
                    temp = a[i];  
                    a[i] = a[j];  
                    a[j] = temp;  
                }  
            }  
        }  
       return a[total-2];  
}  
public static void main(String args[]){  
int a[]={1,2,5,6,3,2};  
System.out.println("Second Largest: "+secondLargest(a,6));  
  
}} 
Try it Yourself

Example 3:

Find the size (or) length of the array:

class Size{
public static void main(String argn[]){
int data [] = {45,87,124,78,127,2};
System.out.println("size of the array is "+data.length);
for(int i = 0; i < data.length; i++)
System.out.println(data[i]);
}
}
Try it Yourself

Example 4:

sort an array:

import java.util.*;
 class Sort_name {
    public static void main(String[] arguments) {
     String names[] = { "Priya", "Sangee", "Nithya", "Sarah"};     
     Arrays.sort(names);
     System.out.println("Sorted array:");
     for (int i = 0; i < names.length; i++)
       System.out.println(i + ": " + names[i]);
   }
 }

Try it Yourself

Example 5:

Reverse an array:

import java.util.Arrays; 
class Reverse{
public static void main(String[] args){   
    int[] array = {1,2,3,4,5,6,7,8,9,10};
   for(int i = 0 ; i < array.length / 2; i++)
  {
    int temp = array[i];
    array[i] = array[array.length - i - 1];
    array[array.length - i - 1] = temp;
  }
    System.out.println("Reverse array : "+Arrays.toString(array));
 }
}
Try it Yourself

Example 6:

print the average of arrays:

class Average {
public static void main(String[] args) {    
       int[] numbers = new int[]{20, 30, 25, 35,60};       
       int sum = 0;
       for(int i=0; i < numbers.length ; i++)
        sum = sum + numbers[i];
        double average = sum / numbers.length;
        System.out.println("Average value of the array elements is : " + average); 
   }
}
Try it Yourself

Example 7:

check if two arrays are equal or not:

import java.util.Arrays;
class Compare {
   public static void main(String[] args) throws Exception {
      int[] ary = {1,2,3,4,5,6};
      int[] ary1 = {1,2,3,4,5,6};
      int[] ary2 = {1,2,3,4};
      System.out.println("Is array 1 equal to array 2? " +Arrays.equals(ary, ary1));
      System.out.println("Is array 1 equal to array 3? " +Arrays.equals(ary, ary2));
   }
}

Try it Yourself

Example 8:

Remove duplicate elements in array:

import java.util.Arrays;
 class Remove_dup {
   static void unique_array(int[] array)
    { 
        int unique_elements = array.length;
        for (int i = 0; i < unique_elements; i++) 
        {
            for (int j = i+1; j < unique_elements; j++)
            {
              if(array[i] == array[j])
                {
                 array[j] = array[unique_elements-1];
                  unique_elements--;
                    j--;
                }
            }
        }
         
          int[] array1 = Arrays.copyOf(array, unique_elements);
         System.out.println();
         System.out.println("Array with unique values : ");
         
        for (int i = 0; i < array1.length; i++)
        {
            System.out.print(array1[i]+"\t");
        }
         System.out.println();
         }
     
    public static void main(String[] args) 
    {        
        unique_array(new int[] {0, 3, 2, 4, 3, 2});
       
      }    
}
Try it Yourself

Example 9:

Find the Length of row in Multidimensional Array:

import java.util.Arrays;
class MultidimensionalArray {
   public static void main(String[] args) {

      int[][] a = {
            {1, 2, 3}, 
            {4, 5, 6, 9}, 
            {7}, 
      };
      
      System.out.println("Length of row 1: " + a[0].length);
      System.out.println("Length of row 2: " + a[1].length);
      System.out.println("Length of row 3: " + a[2].length);
   }
}
Try it Yourself

Example 10:

Find the index position of an array:

class Index {
       public static int  findIndex (int[] my_array, int t) {
        if (my_array == null) return -1;
        int len = my_array.length;
        int i = 0;
        while (i < len) {
            if (my_array[i] == t) return i;
            else i=i+1;
        }
        return -1;
    }
    public static void main(String[] args) {
      int[] my_array = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49};
      System.out.println("Index position of 25 is: " + findIndex(my_array, 25));
      System.out.println("Index position of 77 is: " + findIndex(my_array, 77));
       }
}
Try it Yourself
Share with Friends