2.1 The Array Structure

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ARRAY

2.1 The Array Structure

A one-dimensional array is composed of multiple sequential elements stored in


contiguous bytes of memory and allows for random access to the individual elements. The entire
contents of an array are identified by a single name. Individual elements within the array can be
accessed directly by specifying an integer subscript or index value, which indicates an offset
from the start of the array.

Figure 2.1 A sample 1-D array consisting of 11 elements

Array is a container which can hold a fix number of items and these items should be of the
same type. Most of the data structures make use of arrays to implement their algorithms.
Following are the important terms to understand the concept 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.

2.1.1 Array Representation

Programming languages have various way of declaring variables.

Fig 2.2. Array Representation


2.2.2 Basic Operations of an Array

Following are the basic operations supported by an array.

▪ Traverse − print all the array elements one by one.


▪ Insertion − Adds an element at the given index.
▪ Deletion − Deletes an element at the given index.
▪ Search − Searches an element using the given index or by the value.
▪ Update − Updates an element at the given index.

2.2.3 Array Declaration in Python

from array import *

arrayName = array(typecode, [Iniatializers])

The array is created in python by importing the module array.

Typecodes are used to define the type of value the array will hold. Below is a table of Typecodes.

Typecode Value

b Represents signed integer of size 1 byte/td>


B Represents unsigned integer of size 1 byte

c Represents character of size 1 byte

i Represents signed integer of size 2 bytes

I Represents unsigned integer of size 2 bytes

f Represents floating point of size 4 bytes

d Represents floating point of size 8 bytes

Sample Code: The code below displays all the elements in an array. The for loop is used to
display all the elements in an array. The ‘i’ is the typecode for integer.

from array import *

array1 = array('i', [10,20,30,40,50])

for x in array1:

print(x)

2.2.4 Accessing Array Element

The index of element is used to access each element in array.

Sample Code: In the example, element in index 0 and index 2 are accessed.

from array import *


array1 = array('i', [10,20,30,40,50])

print (array1[0])

print (array1[2])

2.2.5 Insert Operation

Insert operation is to insert one or more data elements into an array. Based on the
requirement, a new element can be added at the beginning, end, or any given index of array. The
buil-in insert() method is used.

Sample Code: In the code example, we insert a value 60 to index 1.

from array import *

array1 = array('i', [10,20,30,40,50])

array1.insert(1,60)

for x in array1:

print(x)

2.2.6 Deletion Operation

Deletion refers to removing an existing element from the array and re-organizing all
elements of an array. The python built-in remove() method is used.

Sample Code: In the code example, we remove the element 40 from an array.
from array import *

array1 = array('i', [10,20,30,40,50])

array1.remove(40)

for x in array1:

print(x)

2.2.7 Search Operation

The search for an array element can be performed using its index or its value.

Sample Code: In the example code, the index of an element 40 is returned. If the value is not
present in the array then program returns error.

from array import *

array1 = array('i', [10,20,30,40,50])

print (array1.index(40))

2.2.8 Update Operation

This refers to updating an existing element from the array at a given index.

Sample Code: In the example code, the element in index 2 is updated with a new value of 80

from array import *


array1 = array('i', [10,20,30,40,50])

array1[2] = 80

for x in array1:

print(x)

You might also like