Share this post on:

Array in Java is a Collection if homogenous data. Array is used to store bulk of data. Array is a container objects that holds a fixed number of data. Size of an Array is established at the time of Creating an Array.

Features of Array:

  • In Java array variable can be declared like other variables with [] after the data type.
  • The variable in the array are ordered and each have an index beginning from 0.
  • Array are objects and thus they occupy memory in Heap Area.
  • The direct super class of an Array types is object.
  • Array are always created at Runtime.

There are two types of Array:

  1. One Dimensional Array
  2. Two Dimensional Array

One Dimensional Array is only having one row. It is also called as Linear Array, Sequential Array, Index based Array.

Syntax of One Dimensional Array :-

  • datatype arrayName[] = new datatype[size of array];
  • data[] arrayname=new datatype[size of array];
  • data []arrayname=new datatype[size of array];

Two Dimensional Array is having both row and column.

Syntax of Two Dimensional Array :-

  • datatype arrayName[][] = new datatype[size of row][size of column];
  • data[][] arrayname=new datatype[size of row][size of column];
  • data [][]arrayname=new datatype[size of row][size of column];

Advantage of Array:

  • Array are used to Store multiple data items of same type by using only single name.
  • We can access any elements randomly by using indexes provided by arrays.
  • Array can be used to implement other data structure like lists, stacks, queues, tree, graph , etc.
  • Primitive type to wrapper classes object conversion will not happen so it is fast.

Disadvantages of Array:

  • Fixed size
  • Memory Wastage
  • Strongly Typed
  • Reduce Performance
  • No methods

Example which demonstrate Array in Java Program.

import java.util.Scanner;

class TestDemo
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println(“Enter the Row and Coloumn”);
int r=in.nextInt();
int c=in.nextInt();
int a1[][]=new int[r][c];
System.out.println(“Enter the data in Array”);
for(int i=0;i<r;i++);
for(int j=0;j<c;j++);
a1[i][j]=in.nextInt();
System.out.println(“My data in Matrix”);
for(int k=0;k<r;k++)
{
for(int l=0;l<c;l++)
{
System.out.print(“\t”+ a1[i][j]);
}
System.out.println();
}
}
}

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

2 Comments

  1. Pingback: How to use Collection Framework in Java - ArzaTechs

  2. Pingback: What are Important topics in java Interview? - ArzaTechs

Leave a Comment

Your email address will not be published. Required fields are marked *