Ra4 Singca Dat2b
Ra4 Singca Dat2b
An array is a collection of items of the same data type stored at contiguous memory
locations. To simplify, envision an array as a staircase where each step holds a value,
akin to your friends standing on different steps. Consequently, you can pinpoint the
location of any friend by merely counting the steps they occupy.
Moreover, arrays serve as invaluable data structures due to their efficiency in both
storing and accessing data. For instance, when we need to maintain a list of numbers,
we can employ an array to keep them all together in one place. Then, we can effortlessly
retrieve any specific number from the list by knowing its position, or index, in the array.
Additionally, arrays offer flexibility, allowing us to store various types of data, including
numbers, strings, objects, and even other arrays. This adaptability renders arrays highly
advantageous for a diverse range of programming tasks.
1-D Array
In the context of 1D arrays, which are essentially linear arrays, you can easily find the
memory address of an element by using a straightforward formula. This type of array is
indexed using a single subscript, which can represent either a row or column index.
To calculate the memory address of an element within a 1D array, you employ the
following formula:
Example: Given the base address of an array A[1300 ………… 1900] as 1020 and the
size of each element is 2 bytes in the memory, find the address of A[1700].
Solution:
Given:
Base address B = 1020
Lower Limit/Lower Bound of subscript LB = 1300
Storage size of one element store in any array W = 2 Byte
Subset of element whose address to be found I = 1700
Formula used:
Address of A[I] = B + W * (I – LB)
Solution:
Address of A[1700] = 1020 + 2 * (1700 – 1300)
= 1020 + 2 * (400)
= 1020 + 800
Address of A[1700] = 1820
2-D Array
When dealing with 2D arrays in Java, you're essentially working with arrays of arrays,
which can be visualized as matrices with rows and columns. There are two common
approaches to finding the memory address of an element within a 2D array: row-major
order and column-major order.
Row-major ordering stores elements in memory by moving through the rows and then
descending to the next row. To calculate the memory address of an element using row-
major order, the following formula is applied:
Example: Given an array, arr[1………10][1………15] with base value 100 and the size of
each element is 1 Byte in memory. Find the address of arr[8][6] with the help of row-
major order.
Solution:
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of column given in the matrix N = Upper Bound – Lower Bound + 1
= 15 – 1 + 1
= 15
Formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
Solution:
Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))
= 100 + 1 * ((7) * 15 + (5))
= 100 + 1 * (110)
Address of A[I][J] = 210
Column-major order, on the other hand, stores elements by moving through the columns
and then proceeding to the next column. To determine the address of an element using
column-major order, you can use the following formula:
Example: Given an array arr[1………10][1………15] with a base value of 100 and the
size of each element is 1 Byte in memory find the address of arr[8][6] with the help of
column-major order.
Solution:
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of Rows given in the matrix M = Upper Bound – Lower Bound + 1
= 10 – 1 + 1
= 10
Formula: used
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1))
= 100 + 1 * ((5) * 10 + (7))
= 100 + 1 * (57)
Address of A[I][J] = 157
Efficient element access: Arrays enable direct and efficient access to any element within
the collection because they store elements in continuous memory locations.
Swift data retrieval: Arrays facilitate rapid data retrieval due to their storage in contiguous
memory locations.
Memory efficiency: Arrays offer an efficient method of memory usage. The compiler only
needs to store the base address of the array and the size of each element to access any
array element.
Versatility: Arrays can accommodate a wide variety of data types, including integers,
floating-point numbers, characters, and even complex data structures like objects and
pointers.
Fixed size: Arrays have a fixed size, which means that you cannot add or remove
elements from an array after it has been created. If you need to add or remove elements
from a collection, you may want to consider using a different data structure, such as a
linked list or a dynamic array.
Inability to store different data types: Arrays can only store elements of the same data
type. To store elements of diverse data types, other data structures such as maps or
structures might be more suitable.
Inefficient memory usage for sparse arrays: If an array contains many empty elements, it
can inefficiently use memory because the compiler allocates memory for the entire array,
even if it's not fully occupied.
References
What is Array - javatpoint. (n.d.). Www.javatpoint.com. https://www.javatpoint.com/what-is-array
Array Data Structure. (n.d.). GeeksforGeeks. https://www.geeksforgeeks.org/array-data-
structure/
What is Array? (2017, October 27). GeeksforGeeks. https://www.geeksforgeeks.org/what-is-
array/
Types of Arrays. (2023, July 20). GeeksforGeeks. https://www.geeksforgeeks.org/types-of-
arrays/
Introduction to Arrays - Data Structure and Algorithm Tutorials. (2022, July 29). GeeksforGeeks.
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/
Calculation of address of element of 1-D, 2-D, and 3-D using row-major and column-major
order. (2021, May 25). GeeksforGeeks. https://www.geeksforgeeks.org/calculation-of-address-
of-element-of-1-d-2-d-and-3-d-using-row-major-and-column-major-order/
GeeksForGeeks. (2022, May 16). Applications, Advantages and Disadvantages of Array.
GeeksforGeeks. https://www.geeksforgeeks.org/applications-advantages-and-disadvantages-of-
array-data-structure/