CSCE 155 - Java
Lab 07 - Arrays & Dynamic Memory
Dr. Chris Bourke
Prior to Lab
Before attending this lab:
1. Read and familiarize yourself with this handout.
2. Review the following free textbook resources:
• Oracle’s Java Tutorial on arrays:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.
html
• Information on Java’s Garbage Collection:
http://www.oracle.com/technetwork/java/javase/tech/index-jsp-140228.
html
• Optionally read up on Perspectives on Garbage Collection: http://www.
oracle.com/technetwork/articles/java/garbagecollection-488837.html
Peer Programming Pair-Up
To encourage collaboration and a team environment, labs will be structured in a pair
programming setup. At the start of each lab, you will be randomly paired up with
another student (conflicts such as absences will be dealt with by the lab instructor).
One of you will be designated the driver and the other the navigator.
The navigator will be responsible for reading the instructions and telling the driver
what to do next. The driver will be in charge of the keyboard and workstation. Both
driver and navigator are responsible for suggesting fixes and solutions together. Neither
the navigator nor the driver is “in charge.” Beyond your immediate pairing, you are
encouraged to help and interact and with other pairs in the lab.
1
Each week you should alternate: if you were a driver last week, be a navigator next,
etc. Resolve any issues (you were both drivers last week) within your pair. Ask the lab
instructor to resolve issues only when you cannot come to a consensus.
Because of the peer programming setup of labs, it is absolutely essential that you com-
plete any pre-lab activities and familiarize yourself with the handouts prior to coming
to lab. Failure to do so will negatively impact your ability to collaborate and work with
others which may mean that you will not be able to complete the lab.
1 Lab Objectives & Topics
At the end of this lab you should be familiar with the following
• Understand dynamic memory management and how the Java garbage collector
operates
• Declare and fill an array with values
• Access an array’s values individually and iterate over the entire array
• Pass an array as a parameter to a function
2 Background
Dynamic memory management involves allocating memory when needed and freeing
up after it is no longer needed. Poorly written programs that do not handle memory
properly can crash or cause memory leaks. A memory leak occurs when all references
to a chunk of memory are lost, but the memory was not properly cleaned up. This can
lead to wasted resources and a substantial slow-down in performance.
Java, like many modern programming languages offers automatic garbage collection. In
the case of Java, the Java Virtual Machine manages memory for you: once an object
is no longer being referenced by another object, it is available to be garbage collected
and its memory freed up for further use by the JVM or by the operating system. In
general, you have no control over when and how garbage collection is performed; the
JVM decides when it is best and how best to go about it.
A typical example in Java:
1 String message = new String("Hello World!");
2 message = null;
The code snippet above allocates new memory space to hold the String object con-
taining the "Hello World!" message. The variable message is holding a reference to
that String object; it points to the memory location where the string is being stored.
2
In the second line when we reassign what the message variable is pointing to (to null)
then all references to that original memory location are lost. In many programming lan-
guages the string would persist in memory until the program ended. In Java, it becomes
available to be garbage collected and may, at some point, be cleaned up by the JVM.
Arrays hold collections of variable values of a certain type ( int or double for example).
In Java, arrays can dynamically allocated using the keyword new . A few examples:
1 int a[] = new int[100];
2 int n = 10;
3 double vector[] = new int[n];
4 double matrix[][] = new int[n][n];
The syntax requires that an integer size be indicated when the array is dynamically
created. Each array is filled with the same default values as the type of variables it
holds. Moreover, as a convenience, Java keeps track of the size of arrays and gives you
access to the size through the length property:
1 for(int i=0; i list = new ArrayList();
2 list.add(10);
3 list.add(20);
4 System.out.println("second element: "+list.get(1));
Read Oracle’s Tutorial on the Collections framework: http://docs.oracle.com/javase/
tutorial/collections/index.html and modify the exercises in this lab to utilize a list
rather than arrays.
4.2 Activity 2
So far we’ve only been working with single dimension arrays, but often times it is im-
portant to represent something like a table or matrix in code. One way to accomplish
this is to use a multidimensional array. You can declare a statically allocated 2D array
with the statement
int matrix[100][50];
You can think of 100 as the number of rows in the table and 50 as the number of columns.
Begin by writing a program that will initialize a matrix with some random numbers and
find the average of each row (you’ll want to use nested for loops). Once you’ve gotten
the hang of accessing the elements in a 2D matrix, write a program that will find the
product of two (square) matrices. To assist you, write a function that will automatically
create a randomly populated two dimensional array and return a reference to it.
6