Partially Filled Arrays

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > More With Arrays > Partially Filled Arrays


Introduction

When working with arrays, you will come across situations where some array indexes contain a value, while others do not. These arrays are said to be 'partially filled'. This section will tell you what a partially filled array looks like and how to handle them.


Partially Filled Arrays

A partially filled array is an array that has indexes that aren't being used to store data. The size of an array is set when it is initialized and cannot be changed afterwards. So, we initialize an array with a size large enough to hold the maximum amount of data that needs to be stored. Figure 1 displays what a typical partially filled array looks like. Notice that indexes 4,5,6 and 7 have not been populated by any data. The lack of data in these indexes is what makes this a partially filled array.

Partially.Filled.Arrays.Fig1.jpg


Defining and Initializing

Partially filled arrays are normally instantiated using a global variable that defines the maximum array size. A local size variable is created to keep track of the current size of your partially filled array. The size variable also allows you to ensure that you are not exceeding the maximum size. Below is an example of the declaration and initialization of a partially filled array.

 FINAL int ARRAY_SIZE = 100;

public static void main(String[] args)
{
   int[] partialArray = new int[ARRAY_SIZE] // use the ARRAY_SIZE variable for the size of your array
   int size = 0; // keeps track of the size of partialArray
} 


Handling Partially Filled Arrays

Handling partially filled arrays is very similar to handling full arrays. The difference is that you must include a size variable and update it when you add and remove data from the array.

To deal with this added complexity, it is important to know how to do two things:

1. Define an array index as empty.

2. Use a Size variable to keep track of the size of your array.

Defining an Empty Index

The obvious value to choose when determining if an index is empty is the default value that a variable is assigned when the variable is created. Recall that when you create a String variable, its default value is null. Meanwhile, for ints, doubles, and chars, their default values are 0. Lastly, the default value for a boolean variable is false.

While using the default value may work well in some cases (using the default works well for Strings), the default value will not work for all cases. In particular, using the default value to define the empty index in an array of ints or doubles can usually lead to problems.

For example, suppose you have a program which stores an assignment mark out of 10 for a group of students in an array of type int. Each index in the array corresponds to one students mark. If you use the default value to define an empty index, it will be impossible to tell the difference between students whose assignments have not been marked, and students who got a mark of 0.

In order to avoid cases where the default value conflicts with possible array values, you need to initialize the array to something different. In the above scenario, the problem of 0 being both an empty index and an input value can be solved by initializing the array index values to -1. The code snippet below shows some example code as to what the initialization code might look like:

 public static void main(String[] args)
{
   int [] gradeArray = new int[30] //gradeArray - there are 30 indexes, one for each student

   for(int i = 0; i < gradeArray.length; i++) //go through all indexes
   {
     gradeArray[i] = -1; //set each index to -1
   }
} 

While initializing the array index values to -1 works well in the assignment mark example, it obviously wouldn't work if -1 was a valid input value. However, in the above example, we are following a more general rule: always define your empty index such that it can not be mistaken for a valid input value.

Inserting & Deleting Data

Most programs will have a separate method for adding and deleting data from a partially filled array. You will need to pass the size variable anytime you need to access the contents of partialArray. When working with a partially filled array where array values are added and removed, your array may fill up to maximum capacity. By comparing the size and ARRAY_SIZE variables, you can quickly determine if new values can be added to the array.

Example: Inserting Data

 public insert(int[] partialArray, int size)
{
   if (size < ARRAY_SIZE)  // check to ensure size is less than ARRAY_SIZE
   {
     partialArray[size] = 50;
     size++;
   }
} 

Example: Deleting Data

 public delete(int[] partialArray, int size)
{
   if (size > 0)
   {
     partialArray[size-1] = 0;  // size-1 is the last index containing data
     size--;
   }
} 


Section Summary

This section showed you how to work with partially filled arrays. Working with partially filled arrays is very similar to working with full arrays except you need to know how to store empty values and keep track of your size variable when inserting and deleting. You should now know what partially filled arrays look like and how to deal with them.

Previous Page: Passing Arrays as Parameters Next Page: Arrays of Strings