You are on page 1of 36

Data Structures and

Algorithms
Lecture – Array

Amir Ali
amir.ali@ciitsahiwal.edu.pk
Outline
Introduction
Array Examples
Declaring Array

String[] cars;

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


Array Storage Structure
Array Storage Structure
Initializing Arrays
• By default, regular arrays are left uninitialized.
• This means that none of its elements are set to
any particular value.
• Their contents are undetermined at the point the
array is declared
• But the elements in an array can be explicitly
initialized to specific values when it is declared,
by enclosing those initial values in braces {}.
Initializing Arrays
• For example:

int foo [5] = { 16, 2, 77, 40, 12071 };

This statement declares an array that can be


represented like this:
Initializing Arrays

int bar [5] = { 10, 20, 30 }; ?


If declared with less, the remaining elements are
set to their default values (which for fundamental
types, means they are filled with zeroes).
Initializing Arrays
int baz [5] = { }; ?

This creates an array of five int values, each


initialized with a value of zero:
Initializing Arrays

int foo [] = { 16, 2, 77, 40, 12071 }; ?


In this case, the compiler will assume
automatically a size for the array that matches
the number of values included between the
braces {}:
Accessing Array
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Outputs Volvo

Accessing Array
Array Operations
Array Operations – Retrieving
Array Operations – Adding
Array Operations – Deleting
Array Operations – Insertion
Implementation of Array Operations
Implementation of Array Operations
Implementation of Array Traversing
Performance of Array Operations
Two Dimensional Array
Two-dimensional Array- Initialization
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

int[][] a = {
int[][] a = new int[3][4]; {1, 2, 3},
{4, 5, 6, 9},
{7},
};
Storage Structure of 2D Array
Storage Structure of 2D Array
String Array
String Array
Performance of 2D Array Operations
Matrix Representation Arrays
Matrix Representation Arrays
Matrix Representation Arrays
3D Arrays
Advantages of Arrays
Limitations of Arrays

You might also like