Array in DataStructure
Array: is a container which can hold fix number of items and these
items should be of same type.
Alternative Definition:
Array: is collection of variables of same type that are referenced by a
common name
Most of the data structure make use of array to implement their
algorithms.
Lowest address of array corresponds to first element.
Highest address of array corresponds to last element.
3.
Introduction
Following are importantterms to understand the concepts of Array.
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index
which is used to identify the element.
Array Representation
Arrays can be declared in various ways
in different languages. For illustration,
let's take JAVA array declaration.
4.
Types of ArrayStructures
Single Dimension Array:
Elements specified by single subscript.
type array_name [size]
e.g int[] multiple = new int[10]; // 1D integer array with size 4
Two Dimensional Array:
An array of more than one dimension is known as a multi-dimensional array. Two of the most
common examples of multi-dimensional arrays are two and three-dimensional array, known as
2D and 3D array.
e.g int[][] multiples = new int[4][2]; // 2D integer array with 4 rows
and 2 columns
5.
Basic Operations onArray
Following are the basic operations supported by an array.
• Traverse − print all the array elements one by one.
• Insertion − add an element at given index.
• Deletion − delete an element at given index.
• Search − search an element using given index or by value.
• Update − update an element at given index.
6.
Insertion Operation
Insert operationis to insert one or more data elements into an array.
Based on the requirement, new element can be added at the
beginning, end or any given index of array.
Here, we see an algorithm of insertion operation.
7.
Deletion Operation
Deletion refersto removing an existing element from the array and re-
organizing all elements of an array.
Here, we see an algorithm of deletion operation.
8.
Search Operation
You canperform a search for array element based on its value or its
index.
Here, we see an algorithm of search operation.
9.
Update Operation
Update operationrefers to updating an existing element from the array
at a given index.
Here, we see an algorithm of update operation.
10.
ARRAY IMPLEMENTATION OFLISTS LIST:
• A list is a sequential data structure, ie. a collection of items accessible
one after another beginning at the head and ending at the tail.
• It is a widely used data structure for applications which do not need
random access Addition and removals can be made at any position in
the list
11.
ARRAY IMPLEMENTATION OFLISTS LIST:
A list can be implemented in two ways:
1. Array list
2. Linked list
12.
JAVA ArrayList
• Javaprovides the ArrayList class, which can be used to store an
unlimited number of objects.
• ArrayList is known as a generic class with a generic type E.
• You can specify a concrete type to replace E when creating
• an ArrayList.
For example:
ArrayList <String> cities = new ArrayList<> ();
Applications of JAVAin Data Structure
• Arrays are used to implement mathematical vectors and matrices, as
well as other kinds of rectangular tables. Many databases, small and
large, consist of one-dimensional arrays whose elements are records.
• Arrays are used to implement other data structures, such as lists,
heaps, hash tables, deques, queues and stacks.
• One or more large arrays are sometimes used to emulate in-program
dynamic memory allocation, particularly memory pool allocation.
Historically, this has sometimes been the only way to allocate
"dynamic memory" portably.
15.
Applications of JAVAin Data Structure
• Arrays can be used to determine partial or complete control flow in
programs, as a compact alternative to (otherwise repetitive) multiple
“if” statements. They are known in this context as control tables and
are used in conjunction with a purpose built interpreter whose
control flow is altered according to values contained in the array. The
array may contain subroutine pointers(or relative subroutine numbers
that can be acted upon by SWITCH statements) that direct the path of
the execution.