There are two types of array.

One Dimensional Array

Syntax for default values:

int[] num = new int[5];

Or (less preferred)

int num[] = new int[5]

Syntax with values given:

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

Or (less preferred)

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

Note: For convenience int[] num is preferable because it clearly tells that you are talking here about array. Otherwise no difference. Not at all.

Example:

Q. WRITE A JAVA PROGRAM TO FIND THE SUM OF ALL INTEGER ELEMENT OF AN ARRAY ? Hint:SAVE THE FOLLOWING CONTENT IN [soft.java] FILE

import java.io.*;
public class soft
{
    public static void main(String arg[])throws IOException
	{
		int a[]={23,45,54,32,67,56,65};
		int sum=0;
		for(int i=0;i<a.length;i++)
		{
			sum=sum+a[i];
		}
		System.out.println("THE SUM OF ARRAY ELEMENT  : " +sum);
	}
}

Q. WRITE A JAVA PROGRAM TO FIND THE LARGEST AND SMALLEST NUMBER IN AN ARRAY OF 5 ELEMENT'S ?

public class soft
{
    public static void main(String[] args) 
	{
		int a[]={12,6,14,10,16};
		int larg=a[0];
		int small=a[0];
		for(int i=0;i<5;i++)
		{
			if(larg<a[i])
			{
				larg=a[i];
			}
			else
			{
				if(a[i]<small)
				{
					small=a[i];
				}
			}
		}
		System.out.println("LARGEST NUMBER : "+larg);
		System.out.println("SMALLEST NUMBER  : "+small);
	}
}         

Multidimensional array

Declaration

int[][] num = new int[5][2];

Or

int num[][] = new int[5][2];

Or

int[] num[] = new int[5][2];

Example:

Q. WRITE A JAVA PROGRAM TO ADD TWO MATRIX ?

import java.io.*;
public class soft
 {
    public static void main(String[] args)throws IOException   
	{
		int fm[][]=new int[2][2];
		int sm[][]=new int[2][2];
		int summ[][]=new int[2][2];
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("ENTER VALUE OF FIRST MATRIX ..");
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				System.out.println("ENTER VALUE AT : fm["+i+"]["+j+"]");
				fm[i][j]=Integer.parseInt(bf.readLine());
			}
		}
		System.out.println("ENTER VALUE OF SECOND  MATRIX ..");
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				System.out.println("ENTER VALUE AT : sm["+i+"]["+j+"]");
				sm[i][j]=Integer.parseInt(bf.readLine());
			}
		}
		System.out.println("SUM OF FIRST AND SECOND  MATRIX ..");
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				summ[i][j]=(fm[i][j]+sm[i][j]);
			}
		}
		System.out.println(" FIRST  MATRIX ..");
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				System.out.print("  "+"["+fm[i][j]+"]");	
			}
			System.out.println("");
		}
		System.out.println(" SECOND   MATRIX ..");
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				System.out.print("  "+"["+sm[i][j]+"]");	
			}
			System.out.println("");
		}
		System.out.println(" sum of first and sedcond matrix  ..");
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				System.out.print("  "+"["+summ[i][j]+"]");	
			}
			System.out.println("");
		}
	}
}

Q. WRITE A PROGRAM TO DIFFERENCE OF TWO MATRIX ?

import java.io.*;
public class soft 
{
    public static void main(String[] args)throws Exception 
	{
		int fm[][]=new int[2][2];
		int sm[][]=new int[2][2];
		int summ[][]=new int[2][2];
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("ENTER VALUE OF FIRST MATRIX ..");
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				System.out.println("ENTER VALUE AT : fm["+i+"]["+j+"]");
				fm[i][j]=Integer.parseInt(bf.readLine());
			}
		}
		System.out.println("ENTER VALUE OF SECOND  MATRIX ..");
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				System.out.println("ENTER VALUE AT : sm["+i+"]["+j+"]");
				sm[i][j]=Integer.parseInt(bf.readLine());
			}
		}
		//CALCULATING DIFFERENCE OF FIRST AND SECOND  MATRIX ...
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				summ[i][j]=(fm[i][j]-sm[i][j]);
			}
		}
		System.out.println(" FIRST  MATRIX ..");
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				System.out.print("  "+"["+fm[i][j]+"]");	
			}
			System.out.println("");
		}
		System.out.println(" SECOND   MATRIX ..");
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				System.out.print("  "+"["+sm[i][j]+"]");	
			}
			System.out.println("");
		}
		System.out.println(" DIFFERENCE OF FIRST AND SECOND  MATRIX ...");
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				System.out.print("  "+"["+summ[i][j]+"]");	
			}
			System.out.println("");
		}
	}
}

Q. Write a Java program to multiply(Product) two matrix ?

class soft
 {
  public static void main(String[] args)  
  {
      int m1[][] = {{1,2,3},{4,5,6}};
	  int m2[][] = {{7,8},{9,0},{11,12}};
	  int mul[][] = new int[3][3];
  
	  System.out.println("Matrix 1 : ");
	  for(int i = 0; i <m1.length; i++) 
	  {
		  for(int j = 0; j <= m1.length; j++) 
		  {
			  System.out.print(" "+ m1[i][j]);
		  }
		  System.out.println();
	  }  
  
	  System.out.println("Matrix 2 : ");
	  for(int i = 0; i < m2.length; i++) 
	  {
		  for(int j = 0; j < m2.length-1; j++) 
		  {
			  System.out.print(" "+m2[i][j]);
		  }  
		  System.out.println();
	  }
  
	  for(int i = 0; i < m1.length; i++) 
	  {
		  for(int j = 0; j < m2.length-1; j++) 
		  {
			  for(int k = 0; k < m2.length; k++)
			  {
				  mul[i][j]=mul[i][j] + m1[i][k]*m2[k][j];
			  }
		  }  
	  }
	  System.out.println("Multiplied  matrix : ");
	  for(int i = 0; i < m1.length; i++) 
	  {
		  for(int j = 0; j < m2.length-1; j++) 
		  {
			  System.out.print(" "+mul[i][j]);
		  }  
		  System.out.println();
	  }
  }
}

Q. WRITE A Java PROGRAM OF BUBBLE SORT ?

import java.io.*;
public class soft 
{
    public static void main(String[] args) throws IOException 
	{
		int a[]={72,11,84,23,51,94,56,62};
		int temp,i,j,k;
		
		//printing un - sorted array ..
		
		System.out.println("UN - SORTED  ARRAY.. ");
		for(i=0;i<8;i++)
		{
			System.out.print("  "+a[i]);
		}
		
		
		//	BUBBLE SORTING ALGO ..
		
		for(i=0;i<a.length;i++)
		{
			for(j=0; j<(a.length-i-1);j++)
			{
				if(a[j]>a[j+1])
				{
					temp=a[j];
					a[j]=a[j+1];
					a[j+1]=temp;
				}
			}
			
			
			
			System.out.println("\n"+(i+1)+" - STEP .....\n");
			for(k=0;k<a.length;k++)
			{
				System.out.print(a[k]+"  ");
			}
		}
		
		System.out.println("\nSORTED ARRAY :  ");
		for(i=0;i<8;i++)
		{
			System.out.print("  "+a[i]);
		}
	}
}

Q. Write a Java program of Selection Sort ?

class soft
{
    public static void main(String arg[])
	{
		int a[]={23,24,15,46,27,18,29};
		int i,j,k,small,temp,pos;
		System.out.println("UN SORTED ARRAY ...");
		for(i=0;i<a.length;i++)
		{
			System.out.print(a[i]+"  ");
		}
		// selection sorting  algorithm ...
		for(i=0;i<a.length;i++)
		{
			small=a[i];
			pos=i;
			for(j=i+1;j<a.length;j++)
			{
				if(a[j]<small)
				{
					small=a[j];
					pos=j;
				}
				
			}
			
			
			temp=a[i];		// smallest element is being swapped 
			a[i]=a[pos];
			a[pos]=temp;
			
			
			
			System.out.println("\n"+(i+1)+" - STEP .....\n");
			for(k=0;k<a.length;k++)
			{
				System.out.print(a[k]+"  ");
			}
		}
		
		System.out.println("SORTED ARRAY ...");
		for(i=0;i<a.length;i++)
		{
			System.out.print(a[i]+"  ");
		}
		
		
	}
}
           

Q. WRITE A PROGRAM OF BINARY SEARCH ?

import java.util.*;

public class soft
{
        public static void main(String[] args) 
        {
                int[] a = {12,23,34,45,56,78,81,91,100};
                int search,foundindex;
                Scanner s=new Scanner(System.in);
                System.out.print("Enter a number to search for: ");
                search = s.nextInt();
                 foundindex= Searchprocess(a, search);
                if (foundindex != -1)
                {
                        System.out.println("Found at index: " + foundindex);
                }
                else 
                {
                        System.out.println("Not Found");
                }
        }

        static int Searchprocess(int[] a, int find) 
        {
                int start, end, midpoint;
                start = 0;
                end = a.length - 1;
                while (start <= end) 
                {
                        midpoint = (start + end) / 2;
                        if (a[midpoint] == find) 
                        {
                                return midpoint;
                        }
                        else if (a[midpoint] < find) 
                        {
                                start = midpoint + 1;
                        }
                        else 
                        {
                                end = midpoint - 1;
                        }
                }
                return -1;
        }
}