Array in JAVA

Array in JAVA


array in java

Array in JAVA:-   

Array in Java is a collection of similar types of data store in sequential order. It is used to create a list

Array is static in nature. Array size is known.

Array cannot be shrinked.

Types of array:-

Array is two types:-

single dimensional array

multidimensional array

Array construction:-

Array construction possible in 3 different step:-

1)array declaration

2)Memory allocation

3)Array initialization

  1. Array declaration:-

The declaration of the array only indicates the name of the array and which types of elements can be store.

At the time of declaration, we cannot specify the size of an array in java.

Ex:-

int x[];

string y;

Student [] s;

int a[10]; //not allowed

  1. Memory allocation:-

In java always allocate memory with the help of  new keyword.

Size is compulsory at the time of memory allocation.

Ex:-        x=new int[size];

y=new Student[size];

y=new string[]; //not allowded

  1. Array initialization:-

If array already initialize then size is not allowed.

x=new int[2] {11,22}; //wrong syntax

x[0]=sc.nextInt();

x[1]=22

Single dimensional array program

import java.util.*;

class Array

{

        static Scanner sc=new Scanner(System.in);

        public static void main(String[] args)

       {

         System.out.println("enter the array size");

         int size=sc.nextInt();

          int arr[]=new int[size];

                                                                               

          input(arr);

           display(arr);

          }

          public static void input(int arr[])

           {

           System.out.println("enter the element for array...");

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

            {

             arr[i]=sc.nextInt();

             }             

           }             

         public static void display(int arr[])

         {

           System.out.println("the array is");

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

            {

              System.out.println(arr[i]+"\t");

           }

        }

}




Output
enter the array size

3

enter the element for array...

23

43

5

the array is

23          

43          

5

Note: Array is a predefined class present in java.util package. This class is not responsible to create new array but it is used to do array manipulation like searching and shorting.

Multi-Dimensional Array program:-

import java.util.Scanner;

class Array2D

{

         static Scanner sc=new Scanner(System.in);

         public static void main(String[] args)

         {

           System.out.println("enter the row and column for array");

           int row=sc.nextInt();

           int col=sc.nextInt();

                               

           int arr[][]=new int[row][col];

           System.out.println("rows:"+arr.length);

            System.out.println("cols:"+arr[0].length);

                               

            input(arr);

             display(arr);

             uppertrangular(arr);

             lowertrangular(arr);

              dimensional(arr);

              }

                private static void uppertrangular(int[][] arr)

                {

                   System.out.println("The uppertriangular  matrix is:");

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

                      {

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

                        {

                          if(i>j)

                           {

                            arr[i][j]=0;

                            }

                           System.out.print(arr[i][j]+"\t");

                           }

                           System.out.println();

                          }

                }

                private static void lowertrangular(int[][] arr)

                {

                     System.out.println("The lowertriangular  matrix is:");

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

                     {

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

                        {

                         if(i>=j)

                          {

                          arr[i][j]=0;

                         }

                         System.out.print(arr[i][j]+"\t");

                         }

                         System.out.println();

                         }

                }

                private static void dimensional(int[][] arr)

                {

                 System.out.println("The dimensional  matrix is:");
                  for (int i = 0; i <arr.length; i++)

                  {

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

                     {

                     if(i<j)

                     {

                     arr[i][j]=0;




                     }

                    System.out.print(arr[i][j]+"\t");

                   }

                    System.out.println();

                  }

                }

             private static void display(int[][] arr)

            {

                  System.out.println("The matrix is:");

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

                   {

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

                       {

                         System.out.print(arr[i][j]+"\t");

                       }

                       System.out.println();

                     }             
                }

                private static void input(int[][] arr)

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

                 System.out.println("enter the element for array"+i);

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

                   {

                   arr[i][j]=sc.nextInt();

                   }

            }

        }

}

Jagged Array Program:-

If the column size of each row is a multidimensional array is different or a distinct then it is known as jagged Array in java.

Example

import java.util.Scanner;




public class Jagged

{

                static Scanner Sc=new Scanner(System.in);

                public static void main(String[] args)

                {

                  System.out.println("Enter the rows...");

                  int row =Sc.nextInt();

                  int arr[][]=new int [row][row];

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

                   {

                   System.out.println("Enter the col................");

                  int col=Sc.nextInt();

                  arr[i]=new int [col];

                  }

                  input();

                  display();

                }

                private static void Display()

                {

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

                   {

                   System.out.println("enter the element for array"+i);

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

                   {

                      arr[i][j]=sc.nextInt();

                    }

                   }


                }

                private static void input()

                {

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

                   {

                  System.out.println("enter the element for array"+i);

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

                   {

                    arr[i][j]=sc.nextInt();

                   }

                  }

                }
}







basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community