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 Concept in Java, Array in Java, What is Array? Jagged Array, Multidimensional array in java?




What is an Array:-

It is a collection of elements of similar data type, array store element using proper sequence, base index of the array will be started from 0 to size-1.


Syntax of Array:-


Datatype arrayname[] = new Datatype[size]

Datatype [] arrayname = new Datatype[size]

int arr[] = new int[5];

int [] arr = new int[5];



Advantage of the array:-


1) We can easily search and sort the elements of the array because array store data using the proper sequence.

2)  Array provides contiguous memory allocation hence we can reduce the extra memory gap.

3)  We can store multiple elements using a single array variable which reduces the extra business code of an application.


The disadvantage of the array:-

1)  Array size is fixed

2)  we can not add, and destroy array elements dynamically.

these limitation has been solved by Collection Framework.




Type of array:-

1 Single Dimension:-    


We can store elements using row, the base index of array element will be started from 0 to size-1.

Datatype arrayname[] = new Datatype[];

Datatype [] arrayname = new Datatype[];


Program of Array:-;
;import java.util.Scanner;
class ArrDemo
{

   public static void main(String args[])
   {
        Scanner sc = new Scanner(System.in);
        int size;
        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();
        }
        for(int i=0;i<size;i++)
        {
          System.out.println(arr[i]);
         
        }


   }


WAP to calculate the sum of array elements?

import java.util.Scanner;
class ArrExample
{

    public static void main(String args[])
    {
          int size;
          int sum=0;
          System.out.println("Enter size");
          Scanner sc = new Scanner(System.in);
          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();

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

 
    }




}
ASSIGNMENT:-

1) WAP to sort the elements of the array in ascending order?

2)  WAP to find the max and second max element in an array?

3)  WAP to merge two arrays in one array?

4)  WAP to display duplicate elements in an array?

1,1,2,3,4,5,2,7,11


package scs;

public class DuplicateArr {

public static void main(String[] args) {
int arr[] = {1,2,3,1,4,5,2,7,2,2,3,1,1,1,4,5,4,4,4,6,7,9,11};
for(int i=0;i<arr.length;i++)
{
int c=0;
for(int k=0;k<i;k++)
{
if(arr[i]==arr[k])
{
c++;
}
}
for(int j=i+1;j<arr.length;j++)
{
if(arr[i]==arr[j] && i!=j)
{
c++;
if(c==1)
{
System.out.println(arr[i]);
}
}
}
}

}

}


1 2

5) WAP to display unique elements in an array?

2 3 4 5 7 11

6) WAP to display prime elements in the array?

The solution to this program:-

import java.util.Scanner;

class ArrDemo
{
   public static void main(String args[])
   {
       int size;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter size");
       size=sc.nextInt();
       int arr[] = new int[size];
       for(int i=0;i<size;i++)
       {
            System.out.println("Enter elements ");
            arr[i]=sc.nextInt();

       }
       
        for(int i=0;i<size;i++)
       {
            int c=0;
           for(int j=1;j<=arr[i];j++)
           {
                if(arr[i]%j==0)
                  c++;
           }
 
          if(c==2)
          System.out.println(arr[i]);
            

       }

  }


}



Optimize program of Prime Number?

import java.util.Scanner;

class ArrDemo
{
   public static void main(String args[])
   {
       int size,i,j;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter size");
       size=sc.nextInt();
       int arr[] = new int[size];
       for(i=0;i<size;i++)
       {
            System.out.println("Enter elements ");
            arr[i]=sc.nextInt();

       }
       
        for(i=0;i<size;i++)
       {
            
           for(j=2;j<arr[i]/2;j++)
           {
                if(arr[i]%j==0)
                  break;
           }
 
          if(j==(int)(arr[i]/2))
          System.out.println(arr[i]);
            

       }

  }


}


2)Multi-Dimension:- 
 

using this array we can store elements using rows and columns, it provides a matrix pattern to display elements.

Syntax of MultiDimension Array;-

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

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





Program Explanation of multidimensional array;-

With user input:-

import java.util.Scanner;
class ArrDemo
{

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


   }

}


Without user input:-

class Matrix
{
    public static void main(String args[])
    {
           int arr[][] = {{1,2},{3,5}};
           for(int i=0;i<2;i++)
           {
               for(int j=0;j<2;j++)
               {
                  System.out.print(arr[i][j]);
               }
               System.out.println();
          }

   }

}


Program of MultiDimenion Array;-

1)  WAP to perform multiplication of the matrix?


2)  WAP to count the total prime element in the matrix?

3)  WAP to find the max element of each row of the matrix?

The solution of max element for each row in the matrix:-

package oops;
import java.util.Scanner;
public class ArrayExample {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r,c;
System.out.println("Enter rows");
r = sc.nextInt();
System.out.println("Enter column");
c=sc.nextInt();
int arr[][] = new int[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println("Enter element for "+i+j+ "index");
arr[i][j] = sc.nextInt();
}
}
System.out.println("Matrix elements is");
for(int i=0;i<r;i++)
{
int max=0;
for(int j=0;j<c;j++)
{
if(max<arr[i][j])
{
max=arr[i][j];
}
System.out.print(arr[i][j]);
}
System.out.println("maximum elements is "+max);
System.out.println();
}

}

}



Limitation of Multi dimension array:-


The number of row elements and column elements will be equal to the matrix. if we want to store a dissimilar number of elements for each row then a multi-dimension array will be failed that problem will solve by Jagged Array.




3  Jagged Array:-

Jagged is the collection of heterogeneous rows, we can provide a different number of column elements in the jagged row.

Java provides a Jagged array Structure similar to a multidimensional array but Jagged array contains a different number of column elements for each row but multi-dimension array contains the same number of column elements for each row.


Example  of a jagged array

1
2 3 4
2 3
5 6 7 8


in this example, a Jagged array contains four rows and for the first row one element, the second row three elements, the third row only two elements, and the fourth row contain four elements.



Syntax of Jagged Array:-

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

int arr[][] = new int[4][];
arr[0] = new int[1];
arr[1] = new int[3];
arr[2]= new int[2];
arr[3]= new int[4];


Normal Jagged Array Example:-



Example of Jagged Array:-

import java.util.Scanner;
class JaggedArr
{
   public static void main(String args[])
   {
        int r,c;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of rows");
        r=sc.nextInt();
        int arr[][] = new int[r][];
        for(int i=0;i<r;i++)
        { 
            System.out.println("enter number of column elements for row "+i);
            c=sc.nextInt();
            arr[i]=new int[c];
            for(int j=0;j<c;j++)
            {
                System.out.println("enter 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();



        }



   }


}




4 Object Array:-

The object is the superclass in java hence it can contain all data type elements using proper sequence.
It is the collection of heterogeneous elements using this array we can store dissimilar types of elements 

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

Object arr[] = {12,"hello",12.34,true};


class ObjectArr
{
   public static void main(String args[])
   {
     Object arr[] = {1001,"manish","CS",45000};
     for(int i=0;i<arr.length;i++)
     {
        System.out.println(arr[i]);
     }
     for(Object o:arr)
     {
          System.out.println(o);
     }

   }

}






Foreach Loop in Java:-

Note:-  for each is used to display the elements of array and collection only .it is the modified form of for loop.

for(var : array)
{

}

array value will be copied to a variable.


for each loop aways will be used to display the element of array and collection. it is also called modified for a loop.







6 Comments

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

  1. public class UniqueElementInArray {

    public static void main(String[] args) {
    int arr[]= {1,1,2,3,4,4,4,5,6,7,8,8};
    for(int i=0;i<arr.length;i++)
    {
    int c=0;
    for(int j=0;j<arr.length;j++)
    {
    if(arr[i]==arr[j]&&i!=j)
    c++;

    }
    if(c==0)
    System.out.println(arr[i]);
    }

    }

    }

    ReplyDelete
  2. WAP to perform multiplication of the matrix?

    public class MultiMatrix {

    public static void main(String[] args) {
    int a[][]= {{1,1,1},{2,2,2},{3,3,3}};
    int b[][]= {{1,1,1},{2,2,2},{3,3,3}};
    int c[][]=new int[3][3];
    for(int i=0;i<3;i++) {
    for(int j=0;j<3;j++) {


    c[i][j]=0;
    for(int k=0;k<3;k++)
    {
    c[i][j]+=a[i][k]*b[k][j];

    }
    System.out.print(c[i][j]+" ");
    }
    System.out.println();


    }

    }
    }

    ReplyDelete
  3. WAP to find the max and second max element in an array?

    public class MaxAndSecondMaxInArray {

    public static void main(String[] args) {
    int nums[]= {4,34,75,3,45,1,100,23};
    int maxOne = 0;
    int maxTwo = 0;
    for(int i=0;i<nums.length; i++)

    {
    if(maxOne<nums[i])
    {
    maxTwo = maxOne;
    maxOne = nums[i];

    }
    else if(maxTwo<nums[i])
    {
    maxTwo = nums[i];
    }
    }
    System.out.println("max number:" + maxOne);
    System.out.println("second max number" + maxTwo);
    }

    }


    ReplyDelete
  4. WAP to sort the elements of the array in ascending order?

    public class AssendingArray {

    public static void main(String[] args) {
    int[] arr=new int[] {5,2,8,7,1};
    int temp=0;
    System.out.println("element of array");
    for(int i=0;iarr[j])
    {
    temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }
    }

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

    }
    }
    }

    ReplyDelete
  5. import java.util.Scanner;

    class arr
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the size of the array");
    int size=sc.nextInt();
    int arr[]=new int[size];

    for(int i=0; i<size; i++)
    {
    System.out.println("enter the elements for the size"+i);
    arr[i]=sc.nextInt();
    }
    for(int j=0; j<size; j++)
    {
    System.out.println("array is" +arr[j]);
    }
    }
    }

    ReplyDelete
  6. import java.util.Scanner;
    class arr
    {
    public static void main(String[]arg)
    {
    int sum=0;
    Scanner sc=new Scanner(System.in);
    System.out.println("ENTER THE ARRAY NO");
    int size=sc.nextInt();

    int arr[]=new int[size];

    for(int i=0; i<size; i++)
    {
    System.out.println("enter the array no for size=" +i);
    arr[i]=sc.nextInt();

    }
    for(int j=0; j<size; j++)
    {
    System.out.println("array =" +arr[j]);

    sum=sum+arr[j];
    }
    System.out.println("sum of array=" +sum);
    }
    }

    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