0% found this document useful (0 votes)
18 views25 pages

Arrays

This document provides an introduction to arrays, a fundamental data structure in programming that allows for efficient storage and manipulation of data. It covers array declaration, initialization, accessing elements, and the use of two-dimensional arrays, along with real-life applications in various fields. Understanding arrays is essential for writing optimized programs and implementing more complex data structures.

Uploaded by

gebiaaaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views25 pages

Arrays

This document provides an introduction to arrays, a fundamental data structure in programming that allows for efficient storage and manipulation of data. It covers array declaration, initialization, accessing elements, and the use of two-dimensional arrays, along with real-life applications in various fields. Understanding arrays is essential for writing optimized programs and implementing more complex data structures.

Uploaded by

gebiaaaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Unit 5: Introduction to Array

Structures
CC2 – Introduction to Computer Programming

Introduction to Basic Data Structures | Array Declaration and Initialization


Accessing Array Elements | Two-Dimensional Arrays
Data Structures
• ways to store and organize data efficiently so that it can
be accessed and modified easily
• form the backbone of programming and algorithms.
Understanding them enables developers to write more
efficient and optimized programs.
Arrays
• a container that holds data (values) of one single type
• allow us to store, organize, and manipulate large amounts
of data efficiently.
Importance of Arrays
1. Efficient Data Management
• Arrays allow for the efficient storage of multiple values in a single
variable.
Importance of Arrays
2. Direct Access to Elements
• Arrays provide indexed access, meaning you can access any
element instantly using its index.
Importance of Arrays
3. Memory Efficiency
• Arrays use contiguous memory locations, which makes them
efficient in terms of memory use.
Importance of Arrays
4. Data Processing and Algorithms
• Arrays form the backbone of many algorithms and are often
used to implement more complex data structures like stacks,
queues, and hash maps.
Array Declaration and Initialization
<data_type>[] <array_name>;
<data_type> <array_name>[];

• dataType can be a primitive data type like: int, char,


double, byte etc. or an object.
• arrayName is an indentifier.
Array Declaration and Initialization
int[] data;
data = new int[5];

• Allocation: Describes how many elements can an array


hold

NOTE: Once the length of the array is defined, it cannot be


changed in the program.
Array Declaration and Initialization
int[] data = {1, 2, 3};

• You can also declare and initialize in one line of code.


Accessing Arrays using Index
• You can easily access and alter array elements by using its
numeric index.
• Indexing starts from 0 and ends at n-1.
Accessing Arrays using Index
int[] data = {10, 20, 30, 40, 50};

• Accessing using index:


<array_name>[<index>];
System.out.println(data[4]);
• Modifying elements:
<array_name>[<index>] = <value>;
data[2] = 15;
Accessing Arrays using Index
• You can use loops to iterate through the elements in an
array.

int[] grades = {99, 98, 97, 96, 95};

for(int i = 0; i < grades.length; i++){


System.out.print(grades[i]);
}
Arrays in Real-Life
• Banking Systems: customer accounts
• Deposits/Withdrawals: Adjust the balance by modifying the value at
the corresponding index.
• Interest Calculation: Traverse the array and apply interest to all
balances.
• Healthcare: patient data
• Analysis: Traverse the array to find patterns or anomalies (e.g., when
the temperature rises or drops).
• Alerts: The system could raise an alert if a temperature crosses a
threshold value.
• Weather Application: temperature readings
• Average Temperature: Calculate the average temperature for the
month.
• Maximum and Minimum: Find the highest and lowest temperatures
recorded in the month.
Two-dimensional Arrays
• arrays inside an array
• allows you to store multiple sets of elements
• organized in rows and columns

String[][] sections = new String[3][4];


String sections[][] = new String[3][4];

ROWS: describes how many arrays there are in the array


COLUMNS: describes how many elements there are for
each row
Accessing 2D Arrays
String[][] students = {{Anna, Ynna, Ina}, {John,
Jan, Johnny}, {Carl, Karla, Kael}};

index 0 1 2

2
Accessing 2D Arrays
index 0 1 2

0 Anna Ynna Ina

1 John Jan Johnny

2 Carl Karla Kael

System.out.println(students[2][1]);
System.out.println(students[0][0]);
System.out.println(students[1][2]);
Accessing 2D Arrays
int[][] grades = {{96, 86, 76}, {83, 73, 93},
{79, 99, 89}};

Give the value:


1. grades[0][2]; 6. grades[1][0];
2. grades[1][1]; 7. grades[0][0];
3. grades[2][1]; 8. grades[2][2];
4. grades[1][2]; 9. grades[0][3];
5. grades[2][0]; 10. grades[0];
Accessing 2D Arrays
int[][] grades = {{96, 86, 76, 66}, {83, 73,
93}, {79, 99}};

• For each array in an array, there are elements. Each array


in an array has a specific number of elements/size of each
row. In order to access all the elements of all the arrays,
we must use a nested for loop.
Accessing 2D Arrays
int[][] grades = {{96, 86, 76, 66}, {83, 73,
93}, {79, 99}};

for(int i = 0; i < grades.length; i++){


for(int j = 0; j < grades[i].length; j++){
System.out.print(grades[i][j] + " " );
}
System.out.println();
}
2D Arrays in Real-Life
• Tic-Tac-Toe

char[][] board = {
{' ', 'X', ' '},
{'O', 'O', 'X'},
{'X', ' ', ' '}
};
2D Arrays in Real-Life
• Spreadsheet Applications

double[][] sales = {{190.3, 340.0, 450.5},


{290.5, 178.0, 130.1}, {80.6, 400.3, 950.3}};
Q1 Q2 Q3
Product 1 190.3 340.0 450.5
Product 2 290.5 178.0 130.1
Product 3 80.6 400.3 950.3
2D Arrays in Real-Life
• Image Processing
B&W Pictures
int[][] image = {
{1, 0, 1, 1},
{0, 1, 0, 0},
{1, 1, 0, 1},
{0, 0, 1, 1}
};
Multi-dimensional Arrays in Real-Life
• Image Processing

RGB Pictures
int[][][] image = {
{{255, 189, 67}, {143, 76, 55}, {93, 0, 2}},
{{78, 33, 97}, {251, 252, 253}, {12, 20, 47}},
{{99, 0, 99}, {86, 47, 178}, {123, 231, 222}},
{{243, 234, 203}, {0, 0, 2}, {97, 30, 198}}
};

You might also like