Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Array:-

It is used to store multiple elements using single variable,Array store similar type of elements in java.

base index of array will be started from 0 .

We can store element using proper sequence with 0 index to size-1.



Advantage of array:-

1 we can easy search and sort the element of array .

2 We can reduce program logic code because array can store multiple values using single variable.

3 We can reduce extra memory space using array because array store data using contiguous memory allocation.



Disadvantage of array:-
1 Size is mandatory in array declaration .

2 we can use similar type of elements only.

 3 we can not destroy array particular block dynamically.

These all problems have been solved by Collection Framework in Java.


Type of Array:-

1) One Dimension Array

2) Multi Dimension Array

3) Jagged Array

4) Object Array


1) One Dimension Array:-

   Using this array we can store element using rows.

    Static Elements

   1.1) Datatype arrayname[] = {value1,value2,....}

       int arr[] = {12,23,34,45,67,11}

   1.2) Datatype arrayname[] = new Datatype[size];

          int arr[] = new int[5]

   1.3)  Datatype [] arrayname = new Datatype[size];
       
           int [] arr = new int[5]; 
 
           int [] arr;
           arr = new int[5];



 1) WAP to find max element in array?

import java.util.Scanner;
class Maxarr
{
   public static void main(String args[])
   {
        Scanner sc = new Scanner(System.in);
        int size;
        int max;
        System.out.println("enter size of array");
        size=sc.nextInt();
        int arr[] = new int[size];
        for(int i=0;i<size;i++)
        {
           System.out.println("enter element for "+i+" index");
           arr[i]=sc.nextInt();

        }
        System.out.println("Array elements is ");
        max=arr[0];
        for(int i=1;i<size;i++)
        {
           if(max<arr[i])
             max=arr[i]; 
           System.out.println(arr[i]);
        }

        System.out.println("max elements is "+max);
         


   }


}


Q WAP to find second max elements in array  using single for loop?

Q WAP to print unique elements in array?

The solution of this program:
class UniqueArr
{
public static void main(String args[])
{
int arr[]={1,2,3,2,5,7,11,7,2};
int i,j;


for(i=0;i<arr.length;i++)
{
   boolean flag =true;

   for(j=0;j<i;j++)
   {
       if(arr[i]==arr[j])
         {
           flag=false;
           break;
         }

   }
   for(j=i+1;j<arr.length;j++)
    {
          if(arr[i]==arr[j])
          {
               flag=false;
               break;
          }
    }


   if(flag)
    System.out.println(arr[i]);

}
}
}

Q WAP to print repeated elements in array but it should be printed once?

Q WAP to print only prime elements in the array using three different methods for input, logic, and display?

import java.util.Scanner;
class Prime
{
    int arr[],size;
    Scanner sc = new Scanner(System.in);
    String result="";
    void accept()
    {
        System.out.println("Enter size of array");
        size=sc.nextInt();
        arr = new int[size];
        for(int i=0;i<size;i++)
        {
           System.out.println("Enter element for "+i+" index");
           arr[i] = sc.nextInt();
        }
    }
    void checkPrime()
    {
       for(int i=0;i<size;i++)
       {
            boolean flag=true;
            for(int j=2;j<arr[i]/2;j++)
            {
                 if(arr[i]%j==0)
                 {
                    flag=false;
                    break;
                 }
            }
            if(flag)
            result+=arr[i]+" ";
       }
    }
    void display()
    {
       System.out.println(result);
    }
   
 public static void main(String args[])
 {
    Prime obj = new Prime();
    obj.accept();
    obj.checkPrime();
    obj.display();
 }
}

Q WAP to check that element is exists or not in an array using a linear and binary search?

int arr[] = {1,2,3,4,5,6,7}
num=5;
mid = arr.length/2;  //4
if(num<arr[mid])
{
   n=0;
   m=mid;
}
else
{
    n=mid+1;
    m=arr.length;
}


for(i=n;i<m;i++)
{
   if(arr[i]==n)
    {

    }

}

Q WAP to sort the elements of an array?

Logic:-

arr[] = {2,23,34,45,67,11}

for(int i=0;i<arr.length;i++)
{
    for(int j=i+1;j<arr.length;j++)
     {
            if(arr[i]>arr[j])
             {
                 int temp=arr[i];
                 arr[i]=arr[j];
                 arr[j]=temp;
             }

     }

}

WAP to sort array in Java?

class SortArr
{
   public static void main(String args[])
   {
        int arr[] = {12,23,11,56,78,9};
        for(int i=0;i<arr.length;i++)
        {
            for(int j=i+1;j<arr.length;j++)
            {
                  if(arr[i]<arr[j])
                   {
                        int temp=arr[i];
                        arr[i]=arr[j];
                        arr[j]=temp;
                        

                   }

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

        }  


    }


}
...........................................................................................................................................

Multidimensional Array:-

using this array we can store elements using rows and columns .Multidimension array row index and column index will from [0][0].


Syntax :-

Datatype arrayname[][] = new Datatype[row][column];

int arr[][] = new int[2][2];

To take input in array
for(int i=0;i<row;i++)
{
    for(j=0;j<column;j++)
    {
       arr[i][j]=sc.nextInt();
    }

}

To display output:-

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

}
2  - 00
3  - 01
4 -  10
5-   11



WAP to display sum of matrix elements?

import java.util.Scanner;
class MatrixArr
{
   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 sum=0;
   
      for(int i=0;i<row;i++)
      {
         int sum1=0;
         for(int j=0;j<column;j++)
         {
           System.out.print(arr[i][j] +" ");
           sum=sum+arr[i][j];
           sum1=sum1+arr[i][j];

         }
         System.out.println("row sum is "+sum1);

      }
      System.out.println("Sum of Matrix is "+sum);

   }


}


Jagged Array:-
..................................................................


Jagged means unstructured array .it contain different number of elements for each row.

means Jagged is the collection of heterogeneous row elements.


Syntax:-

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

int arr[][] = new int[3][];

for(int i=0;i<row;i++)
{
   col=sc.nextInt();
   arr[i]=new int[col];
   for(int j=0;j<col;j++)
   {
       arr[i][j]=sc.nextInt()
   }

}
for(int i=0;i<row;i++)
{

   for(int j=0;j<arr[i].length;j++)
   {
       sout(arr[i][j];
   }
 
}




Complete Solution:-

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

         }

      }

      int sum=0;
   
      for(int i=0;i<row;i++)
      {
         int sum1=0;
         for(int j=0;j<arr[i].length;j++)
         {
           System.out.print(arr[i][j] +" ");
           sum=sum+arr[i][j];
           sum1=sum1+arr[i][j];

         }
         System.out.println("row sum is "+sum1);

      }
      System.out.println("Sum of Matrix is "+sum);

   }


}






   

 



Program to check palindrome?

class UniqueArr
{
public static void main(String args[])
{
char arr[]={'a','a','a','a','a','a'};
int i,j;

boolean flag =true;
for(i=0,j=arr.length-1;i<arr.length/2;i++,j--)
{
       if(arr[i]!=arr[j])
         {
           flag=false;
           break;
         }

 }
   if(flag==false)
    System.out.println("Not pallindrom");
   else
    System.out.println("pallindrom");



}
}




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

Program to implement String in Java ?

class UniqueArr
{
public static void main(String args[])
{
//char arr[]={'a','a','a','a','a','a'};
String arr = "naman";
int i,j;

boolean flag =true;
for(i=0,j=arr.length()-1;i<arr.length()/2;i++,j--)
{
       if(arr.charAt(i)!=arr.charAt(j))
         {
           flag=false;
           break;
         }

 }
   if(flag==false)
    System.out.println("Not pallindrom");
   else
    System.out.println("pallindrom");



}
}
 




ASSIGNMENT:-

WAP to convert char array and string from upper case to lower case?

WAP to count total vowel and consonant in char array and String?

WAP to validate mobile number in char array and string ?

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

Object array:-

It is the collection di-similar type of element because Object type can contain any datatype values.

 Object is the super class in java means all classes will be inherited from Object.

It is used to map database table record .

Object identifier[] = {value1,value2,....}

Object arr [] = {1001,'"xyz","CS",90000 };

for(int i=0;i<arr.length;i++)
{

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

foreach loop:-  It is used to display array elements.

for(datatype var:arrayname)
{
     System.out.println(var)
}

int arr[] = {1,2,3,4,5,7,9,6};

for(int a:arr)
{
    System.out.println(a);
}
Object arr[] = {"1",2,3,4,5,7,9,6.0,true,"hello"};

for(Object a:arr)
{
    System.out.println(a);
}


Complete Program of Object Array:-

class ObjectArr
{
   public static void main(String args[])
   {
    Object arr[] = {"1",2,3,4,5,7,9,6.0,true,"hello"};

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

    for(Object a:arr)

    {

    System.out.println(a);

    }

   }

}


























2 Comments

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

  1. This comment has been removed by the author.

    ReplyDelete
  2. Second Max element :

    import java.util.Scanner;
    class secondmax
    {

    public static void main(String[] args) {

    int arr[] = new int[5];
    Scanner sc= new Scanner(System.in);
    for(int i=0;i<5;i++)
    {
    arr[i]=sc.nextInt();
    }
    int max=arr[0];
    int smax=arr[0];
    for(int i=0;i<5;i++)
    {
    if (arr[i]>max)
    {
    smax=max;
    max=arr[i];

    }
    else if(arr[i]>smax)
    {
    smax=arr[i];

    }
    }

    System.out.println("\nSecond maximum number is:" + smax);

    }
    }

    ReplyDelete

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