Search This Blog



It is a collection of elements of similar and dissimilar data type both.java provide integer array for similar type and Object array then dissimilar type element.


Type of Array:-

1.1) Single Dimension Array:-

using this array we can store element using rows. base index of single dimension array will be started from 0.


Datatype arrayname[] = new datatype[size];
int arr[] = new int[5];

Datatype [] arrayname = new Datatype[size];

int [] arr = new int[5];

Datatype arrayname[] = {value1,value2,value3,...}

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

Object arr[] = {"C","CPP",1003,12.34}


Program of Split one Array to two different Sub-Array?

class Splitarr
{
public static void main(String args[])
{
  System.out.println("array split program");
int arr[]= {2,11,34,56,67,78,89,91,12,11,12};

int size = arr.length;

int size1 = size/2;  //5
int size2 = size-size1;  //6

int arr1[] = new int[size1];
int arr2[] = new int[size2];

for(int i=0;i<arr.length;i++)  //4
{
    if(i<size1)
    {
      arr1[i]=arr[i];
      System.out.println("First"+arr1[i]);
    }
   else
    {
    arr2[i-size1]=  arr[i];
    System.out.println("Second"+arr2[i-size1]);
   }

}
}
}

/* program to sort the element of array in desc order */

class SortArr
{
   public static void main(String args[])
   {
      int arr[] = {1,23,11,67,87,96,32};
      int t;
      for(int i=0;i<arr.length;i++)
      {
          for(int j=i+1;j<arr.length;j++)
           {
                 if(arr[i]<arr[j])
                  {
                         t=arr[i];
                         arr[i]=arr[j];
                         arr[j]=t;
                     
                  }


           }
           System.out.println(arr[i]);

       }

    System.out.println(arr[1]);

   }


}

/* Program to find unique element in array */

class UArr
{
public static void main(String args[])
{
int arr[] = {1,1,4,5,8,4};
for(int i=0;i<arr.length;i++)  //1
{
     int c=0;
    for(int j=0;j<arr.length;j++)   // 0 to 5
     {
              if(arr[i]==arr[j] && i!=j)
                  c++;
      }
      if(c==0)
      System.out.println(arr[i]);
   
}

}
}









1.2) Multi Dimension Array:-

  Using this array we can store data using rows and columns. we can implement matrix operation, Datatable manipulation using MultiDimension Array.

Computer graphics also use a multidimensional array to draw the graphical component.

datatype arrayname[][] = new datatype[row][col];

datatype [][] arrayname = new datatype[row][col];

datatype arrayname[][] = {{value1,value2},{value1,value2}};

Q) WAP to InputData in MultiDimension Array and display into matrix form?


import java.util.Scanner;
class Matrix
{

   public static void main(String args[])
   {
       int row,column;
       Scanner sc = new Scanner(System.in);
       System.out.println("enter row");
       row=sc.nextInt();
       System.out.println("enter column");
       column=sc.nextInt();
       int arr[][] = new int[row][column];
       for(int i=0;i<row;i++)
       {
            for(int j=0;j<column;j++)
            {
              System.out.println("enter element for "+i+j+ " index");
              arr[i][j]=sc.nextInt();
            }

        }

       for(int i=0;i<row;i++)
       {
            for(int j=0;j<column;j++)
            {
              System.out.print(arr[i][j] +" ");
           
            }
            System.out.println();

        }
     
   


    }


}

//WAP to find max element in each row and complete matrix?

import java.util.Scanner;
class Matrix
{

   public static void main(String args[])
   {
       int row,column;
       Scanner sc = new Scanner(System.in);
       System.out.println("enter row");
       row=sc.nextInt();
       System.out.println("enter column");
       column=sc.nextInt();
       int arr[][] = new int[row][column];
       for(int i=0;i<row;i++)
       {
            for(int j=0;j<column;j++)
            {
              System.out.println("enter element for "+i+j+ " index");
              arr[i][j]=sc.nextInt();
            }

        } 

      int max1=0;
       for(int i=0;i<row;i++)
       {
            int max=0;
            for(int j=0;j<column;j++)
            {
              if(max<arr[i][j])
                max=arr[i][j];
              if(max1<arr[i][j])
                max1=arr[i][j];
              System.out.print(arr[i][j] +" ");
              
            }
            System.out.println("M:-"+max);

        } 
       System.out.println("Maximum of complete matrix"+max1);
       
     


    }



}




1.3) Jagged Array:-  Jagged Array is the collection of heterogeneous rows. means we can store a different number of column elements for each row.

datatype arrayname[][] = new datatype[row][];
arrayname[0] = new int[size];
...


Program to assign input and display output in Jagged Array?
Complete Program of Jagged Array:-

import java.util.Scanner;
class JaggedArr
{
     public static void main(String args[])
     {
        Scanner sc  = new Scanner(System.in);
        int r,c;
        System.out.println("enter row");
        r=sc.nextInt();
        int arr[][] = new int[r][];
        for(int i=0;i<r;i++)
        {
             System.out.println("enter column size");
             c=sc.nextInt();
             arr[i]=new int[c];
             for(int j=0;j<c;j++)
             {
                   System.out.println("enter row and column element for "+i

+j+" index ");
                   arr[i][j]=sc.nextInt();
              }

        }

       for(int i=0;i<r;i++)
        {
            for(int j=0;j<arr[i].length;j++)
            {
                  System.out.print(arr[i][j]+" ");
            }
            System.out.println(); 

       }


     }



}



1.4) Object Array:-  using this we can store dissimilar type of elements using the proper sequence.


1 PREDEFINE OBJECT CLASS ARRAY:-

Object arryname[] = {values,...}


2 User Define Object Array:-

Classname ref[] = new Classname[size];

Student arr[] = new Student[5];



Advantage of Array:-

1 we can store element using proper sequence hence we can easily search and sort element from an array.

2 array variable can store multiple values using a single identifier hence we can reduce extra program code to implement code.

3 Array reduce extra memory gap or allocation if we use normal variable.



Disadvantage:-

1 Array size is fixed means without size determination we can not create an array.

2 We can not add, append and delete an element of array dynamically.


(this limitation of the array has been solved by the collection framework in java.)



Q) WAP to Check Palindrome  using Char Array?

Logic
char arr[] = {'t','s','s','s','s','s','s','t'};

boolean flag=true;
for(int i=0;i<arr.length/2;i++)   0 t0 4
{
    if(arr[i]!=arr[(arr.length-1)-i])   arr[1]!=arr[3]
    {
      flag==false;
      System.out.println("not pallindrom");
      break;
   
    }

}

if(flag)
sout("palindrom");


Q) WAP to print reverse of Char Array?

Q) WAP to count total vowel and consonant in Char Array?

Q) WAP to  replace char array each char to next consecutive char?

Q) WAP to convert Char Array to upper case to lower case and lower case to Upper case



.................................................................................................................................................
Q) WAP to multiply two matrix ?

Q) WAP to find second max element in each row of matrix?

Q) WAP to count total prime element in matrix?

Q) WAP to find average of Matrix rows?

Q) WAP to sort matrix rows ?

............................................................................................................

Q) WAP to Find Max element in Jagged Array?

Q) WAP to sort row element of Jagged Array?

Q) WAP to reverse Jagged row element of Array?

Q) WAP to count total even and odd number in Jagged Array?












Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post