Lecture 12 - Array
Lecture 12 - Array
Array
• Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
• The lowest address corresponds to the first element and the highest address to the last
element.
• They can be used to store a collection of any type of primitive data type, including int,
float, double, char, etc.
Array Types
• There are 2 types of arrays in Java programming:
1. Single Dimensional Array (1D)
• A 1D array is a linear data structure that stores elements of the same type in a
contiguous memory block.
• Elements in a 1D array are accessed using a single index.
2. Multidimensional Array
• It represents a grid-like structure with rows and columns.
• Elements in a multidimensional array are accessed using two indices, representing
the row and column (In 2D arrays).
1D vs. 2D
1D
Number of rows: 1 Number of columns: 10
2D 2D
Number of rows: 2
Number of columns: 5
Number of rows: 4
Number of columns: 2
1D Array Declaration
● Syntax:
dataType arrayName[arraySize];
● Example:
○ int marks[] = new int[5] ;
Index 0 1 2 3 4
Elements
1D Array Initialization
int marks[5] = {4, 1, 8, 7, 9};
OR
int marks[] = {4, 1, 8, 7, 9};
No need to specify the size. The compiler knows its size is 5 as we are initializing it with 5 elements.
Index
0 1 2 3 4
Value
Array
Elements 4 1 8 7 9
2D Array Declaration
● Syntax:
dataType arrayName[][] = new int [rows][columns] ;
● Example:
int studentMarks[][] = new int [2][5];
No of rows: 2 No of columns: 5
Column
0 1 2 3 4
Index -->
Row
0
index
Row
1
index
2D Array Initialization
int marks [2][5] = { {4, 1, 8, 7, 9}, {5, 9, 2, 0, 3} };
OR
int marks [][5] = { {4, 1, 8, 7, 9}, {5, 9, 2, 0, 3} };
Row
0 4 1 7 8 9+
index
Row
1 5 9 2 0 3
index
Array Out of Bound
• Occurs when the program tries to access an index that does not exist.
• If we declare an array of size 10, then the array will contain elements from index
0 to 9
• However, if we try to access the element at index 10 or more than 10, it will result
in Undefined Behavior.
Advantages
• Easy to declare and use.
• Can store multiple data items in similar type at once.
• Flexibility.
• Arrays can be used to store a variety of data types, such as integers, floating-point
numbers, characters, and even complex data structures such as objects.
• Can be used to implement efficient sorting algorithms, such as quicksort.
• Support for random access of elements.
• Can be used to quickly retrieve data, since the elements of the array are stored in
contiguous memory locations.
• Fixed Size.
• An advantage in situations where you want to ensure a specific amount of memory is
allocated and don't want the array to grow beyond that size unintentionally.
Disadvantages
• Fixed Size:
• Arrays have a predetermined size that cannot easily be changed during runtime.
• Memory Wastage:
• Allocating more space than needed for an array may lead to inefficient memory usage.
• Inefficient Insertion and Deletion:
• Inserting or deleting elements within an array can be inefficient due to the need for shifting
elements.
• Sequential Access:
• Finding specific elements in an array requires sequential access, which can be inefficient for large
datasets.
• Difficulty in Resizing:
• Resizing arrays often involves creating a new array and copying elements, which can be resource-
intensive.
Important
• The array's indexes begin at 0.
• Therefore, the first item saved at index 0.
• The final element of an array with size n is kept at index (n-1).
• An array's elements have sequential addresses.
• If array is stored with number of elements that is less than the array size, the
compiler fills in the empty spaces with random values. This random value
frequently appears as 0.
END