Flat Preloader Icon

Array Data Structure

Overview

  1. Types of variables in Java
  2. About Arrays
  3. When to use Arrays ?
  4. Conventional approach to solve a problem?
  5. Conclusion
  6. Array data structure

Types of Variables In Java

  1. Value type variables ( ordinary variables of primitive types)
  2. Nullable type variables (Reference variables of non primitive type
    or array reference variables of any type.
  • int x;
    float y;
    Student s1;
    int[]a;
    Student[]s;

About Arrays

  • Array is a linear collection of similar
    elements.
  • Array elements are indexed.
  • Array in java is always created dynamically.
  • [ ] is called subscript operator.
  • All arrays have length property which
    contains the length of the array.
  • int[]a=new int[5];
    a->length[5]
  • Accessing Array elements is fast
  • It takes constant time to access any
    item of the array it index is
    known.

When To Use Arrays?

  • whenever group of related data is need to be stored.
  • whey data is in a group of groups

To solve a programming problem, you have to store marks of 100 students .How can you implement it?

  • int [] a = new int[100];

Suppose you have created an array as :

  • int []a = new int[100];

And assume that you have stored some out of 100 data in this array .

Now answer following questions

  1. How many elements are stored in the array ?
  2. IF suppose 10 elements are stored and then I want to store one more element at index 2 . Can we do it as  a[2]=data;
  3. How to guard against overflow or underflow?
  4. How to delete an element ?

Conclusion

  • Normal array is not good enough to
    efficiently handle such situation
  • we need to keep extra information
    like ,index of last filled block
    ( assume elements are filled from left
    to right) to track the number of elements
    present in the array.

  • We need to create an array data structure in Java.
  • Define a class Array with appropriate number of variables and functions .