Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Array Data Structure 4.2.1 Connecting Computational Thinking and Program Design 1

Uploaded by

dia
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Array Data Structure 4.2.1 Connecting Computational Thinking and Program Design 1

Uploaded by

dia
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Program coding so far..

• 1: Syntax, rules, variables, Input, output


• 2: Arithmetic and logical operations
• 3: Selection (IF-ELSE)
• 4: Iteration/Loops (FOR/WHILE)
• 5: Methods and Parameters
• 6: Tracing Algorithms
• 7: Objects and classes
• 8: Arrays
• 9: Applying methods using Arrays
• 10:Sorting and searching Algorithms.
Starter:

We have learned, Variables, Data types, Arithmetic and logical operators, methods,

Parameters and basic program structures using Java Language

A real-world scenario:

"You are building a program to manage the scores of 10 students in a quiz. How

would you store and access these scores efficiently?"


Arrays vs Variables

Array in Java is index-based, the first element of the array is

stored at the 0 th index, 2nd element is stored on 1st index and so

on.
Initialising an array
Array Data Structure:

An array is a linear data structure consisting of a collection of elements, each identified by an index or a key. Arrays

are widely used in computer science and real-life applications due to their simplicity, efficiency, and ability to store

multiple data elements in a structured format.


Features of Arrays

Fixed Size: The size of an array is determined at the time of its creation and cannot be changed.

Indexing: Elements are stored in contiguous memory locations, accessible via indices (0-based or 1-based).

Homogeneity: Arrays store elements of the same data type.


Types of Arrays:
Efficient Access: Random access is possible, making retrieval of elements fast (O(1) time complexity).

• One-dimensional array: A linear sequence of elements (e.g., [1, 2, 3, 4]).


Applications of Arraysarrays:
Multi-dimensional in Real Life: containing
Arrays Data Storage ande.g.,
arrays, Organization:
2D arrays (matrices) and 3D arrays.

Example: Storing student grades or temperatures recorded over a week.

Benefit: Arrays help organize data in a compact, indexed structure, facilitating quick access and updates.
//Java Program to illustrate how to declare, instantiate, initialize

//and traverse the Java array.

class Testarray

public static void main(String args [] )

int a[ ]=new int [5] ; //declaration and instantiation

a [0]=10; //initialization

a [1]=20;

a [2]=70;

a [3]=40;

a [4]=50;
What could go wrong?
num [ 0 ] always OK
num [ 9 ] OK (given the above declaration)
num [ 10 ] illegal (no such cell from this declaration)
num [ -1 ] always NO! (illegal)
num [ 3.5 ] always NO! (illegal)
Array length
• When dealing with arrays, it is advantageous to
know the number of elements contained within the
array, or the array's "length". This length can be
obtained by using the array name followed
by .length. If an array named numbers contains 10
values, the code numbers.length will be 10.

** You must remember that the length of an array


is the number of elements in the array, which is one
more than the largest subscript.

http://mathbits.com/MathBits/Java/arrays/Declare.htm
Program Implementation using Java

Example 1:

public class Arrays

public static void main(String[] args)

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

System.out.println ( cars [0] );


Example 2: To find the length of the Array( Number of elements)
}

} public class length

public static void main(String[] args)

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

System.out.println(cars.length);
Practice :1
Real-Life Example

To demonstrate a practical example of using arrays, let's create a program that calculates the average of

different ages:

// An array storing different ages

int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};

float avg, sum


Complete the = 0;
program
Challenge 2:

create a program that finds the lowest age among different ages:

// An array storing different ages

int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};

// Create a 'lowest age' variable and assign the first array element of ages to it

int lowestAge = ages[0];


Parallel arrays
Parallel array applications
Parallel arrays in Java
for(int index = 0; index < dogname.length; index++)
{
System.out.println(dogname[index]);
System.out.println(round1[index]);
System.out.println(round2[index]);
}
Searching an array using a flag
Sorting an array (Bubble Sort)
2D arrays
Declaring a 2D array in Java
int[ ][ ] arrNumbers = new int[6][5];

6 = number of rows (DOWN)


5 = number of columns (ACROSS)
Instantiating a 2D array
Example: Fill a 2D array with “X”

Nothing!

You only put data in,


Output
not printed it!
Common mistake: printing a 2D array

You can’t just print the array name,


You have to print every element in the
array separately!

Output
Correct way: printing a 2D array

Ou
t pu
t
Common 2D array tasks

You might also like