What is Array?

An array is an indexed collection of fixed number of homogeneous data elements.

Main Advantage:

  1. In array we can represent multiple values with a single variable.
  2.  Reusability of the code will be improved.

Limitations of object type arrays:

  1. Arrays are fixed in size: Once we created an array with some size there is no chance to increase and decrease its size based on our requirement, Hence to use arrays compulsory we should know the size in advance which may or may not possible.
  2. Arrays can hold only homogeneous data elements.

Example :

Student[] s = new Student; (Correct)

S[0]             = new Customer; (Wrong)

Compiler Exception: Incompatible type

Found: Customer

Required: Student

 

But we can resolve this problem by using object arrays.

Object[] o = new Object[1000];

o[0]           = new Student[];

o[1]           = new Customer[];

 

Arrays Concept is not implemented based on some standard data structure hence readymade method support is not available for every requirement we have to write the code explicitly which is complexity of programming.

Individual Variable Approach

 

Huge number of variable

 

Int x=10;

Int y-10;

Int z=10;

Array

 

Student [] S = new Student[1000];

 

0 0 0

0       1         2       3     ————————-999

S

We can represent huge no. of variable by using a single variable

  • Array concept is not implemented using any datastructure.

Suppose we want to insert element in some sorting order than who will responsible to write the sorting code programmer

But in collection readymade method support is available for sorting we can use Tree Set (Automatically element will insert in some sorting order)

 

  • Second, If we want to search whether the particular element is present or not. So in arrays we will have to write the searching codes.

But in collection we have one readymade method called contains()

About Arrays:

  1. Fixed in size.
  2. Homogenous elements
  3. Underlying DS
  4. 4 Contains()

TO OVERCOME ALL THESE PROBLEMS WE SHOULD GO FOR COLLECTIONS CONCEPT

Leave a Reply