Professional Documents
Culture Documents
ALGO - LEAP Module 4
ALGO - LEAP Module 4
OVERVIEW
This module discusses the concept of dynamic arrays. Various techniques and algorithms
to manipulate dynamic arrays are also covered.
MODULE OBJECTIVES:
42 | P a g e
PRE-TEST
Name: Score:
Directions: Encircle ‘T’ if the statement is correct. Otherwise, encircle ‘F’. (10 points)
T | F 1. A static array is a variable-size list data structure that allows elements to be added
or removed.
T | F 5. Resizing a dynamic array involves allocating a new array and copying each
element from the original array.
T | F 6. If the length of an array is n, the first element of the array will be arrayName[0]
and the last element will be arrayName[n-1].
T | F 9. If we did not store any value to an array, the array will store some default value (0
for int type and true for boolean type) by itself.
T | F 10. After deleting an element from the dynamic array, all elements at the left side of
the selected index are shifted to the right.
43 | P a g e
CONTENTS
An array is a container object that holds a fixed number of values of a single type. The length of
an array is established when the array is created. The length is fixed after creation.
A dynamic array is a variable-size list data structure that allows elements to be added or
removed. Dynamic arrays overcome a limit of static arrays, which have a fixed capacity that
needs to be specified at allocation.
A simple dynamic array can be constructed by allocating an array of fixed-size, typically larger
than the number of elements immediately required. The elements of the dynamic array are stored
at the start of the underlying array, and remaining positions towards the end of the array are
reserved or unused. Elements can be added at the end of the dynamic array by using reserved
space until the space is completely consumed. The underlying fixed-size array needs to be
increased in size when further elements have to be added after all the space is consumed.
Typically resizing is expensive as it involves allocating a new array and copying each element
from the original array (costs O(n) time).
A fixed-size array will suffice in scenarios where the maximum logical size is fixed. A dynamic
array will be needed when the maximum logical size is unknown initially, or likely to change.
As discussed in the previous section, elements are added at the end of an array. A new array
(typically double the original array size) is created and data is copied from original array to the
new one after the allocated space is consumed.
To delete an element from a dynamic array, the element at the index specified by the user is
removed. After that, all elements at the right side of the index are shifted to the left.
44 | P a g e
Resize an Array
To illustrate the concepts discussed earlier, let’s consider the sample program below:
DynamicArray.java
// get method for the element at the index specified by the parameter
public int getElement(int index){
return array[index];
}
if(index>=size || index<0)
System.out.println("No element at this index");
else {
for(int i=index;i<size-1;i++)
array[i] = array[i+1];
array[size-1]=0;
size--;
}
}
}
Test.java
import java.io.*;
46 | P a g e
// creates the dynamic array
DynamicArray dynamicArray = new DynamicArray(initCapacity);
47 | P a g e
Below is the output of the program:
48 | P a g e
POST-TEST
Name: Score:
I. True or False. Encircle ‘T’ if the statement is correct. Otherwise, encircle ‘F’. (10 points)
T | F 4. Resizing a dynamic array involves allocating a new array and copying each
element from the original array.
T | F 6. After deleting an element from the dynamic array, all elements at the right side of
the selected index are shifted to the left.
T | F 9. If we did not store any value to an array, the array will store some default value (0
for int type and false for boolean type) by itself.
II. Output Tracing. Trace and write the output of the following Java statements. If the
statements will result to a compile-time or runtime error, do not write anything except ERROR
as your answer. Write your answers in the space provided. (2 points each)
49 | P a g e
2. int[] numbers = {7,3,8,-11,3};
for(int i=1; i<numbers.length-1; i++) {
numbers[i-1] = ++numbers[i] + numbers[i+1];
System.out.print(numbers[i-1]+" ");
}
50 | P a g e
MODULE TEST
Name: Score:
I. True or False. Encircle ‘T’ if the statement is correct. Otherwise, encircle ‘F’. (10 points)
T | F 3. A static array is a variable-size list data structure that allows elements to be added
or removed.
T | F 5. Resizing a dynamic array involves allocating a new array and copying each
element from the original array.
T | F 6. After deleting an element from the dynamic array, all elements at the right side of
the selected index are shifted to the left.
T | F 7. As with variables of other types, the declaration of an array does not actually create
an array; it simply tells the compiler that this variable will hold an array of the
specified type.
T | F 9. You can access the length of an array via its size property.
T | F 10. String[] strings = {"one", "two", "three"}; is a valid statement to declare, create and
initialize an array.
II. Programming
Problem
Write a program to insert an element (specific position) into an array. (32 points)
Sample Output
Grading Rubric
52 | P a g e
REFERENCES
Textbook:
[1] Malik D. S. 2019. C++ Programming including Data Structures. Cengage Learning.
Online Resources:
[2] JavaCodeGeeks. 2019. Dynamic Array Java Example. Retrieved May 10, 2020 from
https://examples.javacodegeeks.com/dynamic-array-java-example/
[3] TutorialsPoint. 2020. Data Structures - Algorithms Basics. Retrieved May 10, 2020
https://www.tutorialspoint.com/data_structures_algorithms/algorithms_basics.htm
[4] Friesen J. 2017. Data structures and algorithms in Java, Part 1: Overview. Retrieved May
10, 2020 from https://www.javaworld.com/article/3215112/java-101-datastructures-and-
algorithms-in-java-part-1.html
53 | P a g e