Difference between revisions of "Working with Partially Filled Arrays"

From CompSciWiki
Jump to: navigation, search
Line 1: Line 1:
{{Template:1010Topic|Chapter_TOC=[[More With Arrays]] > [[Working with Partially Filled Arrays]]|Introduction=When working with arrays, you will come across situations where some array indexes do not have useful values. These arrays are said to be 'partially filled'. This section will tell you why partially filled arrays occur, what a partially filled array looks like, and how to handle partially filled arrays.|Overview=Working with partially filled arrays requires that you have defined what an empty index is, and that you increment/decrement a counter whenever you insert/remove values from the array.}}
+
{{Template:1010Topic|Chapter_TOC=[[More With Arrays]] > [[Working with Partially Filled Arrays]]|Introduction=When working with arrays, you will come across situations where some array indexes do not have useful values. These arrays are said to be 'partially filled'. This section will tell you why partially filled arrays occur, what a partially filled array looks like, and how to handle partially filled arrays.|Overview=Working with partially filled arrays requires that you have defined what a empty index is, and that you increment or decrement a counter whenever you insert or remove values from the array.}}
  
 
==Why Partially Filled Arrays Occur==
 
==Why Partially Filled Arrays Occur==
Line 5: Line 5:
 
Partially filled arrays occur because once an array size it set, the array size cannot be changed. Therefore, we generally pick an array size that is large enough to hold the ''potential'' number of values that need to be stored. For example, if you usually need to store 20 values in an array, but sometimes you might need to store 40, then you need to make an array of size 40. However, this means that on average, half the array indexes will be empty.  
 
Partially filled arrays occur because once an array size it set, the array size cannot be changed. Therefore, we generally pick an array size that is large enough to hold the ''potential'' number of values that need to be stored. For example, if you usually need to store 20 values in an array, but sometimes you might need to store 40, then you need to make an array of size 40. However, this means that on average, half the array indexes will be empty.  
  
Other reasons why partially filled arrays occur include cases where array indexes are only filled with values once certain conditions are met. Whenever there are filled indexes mixed with empty indexes, the array will be partially filled. For example, say you wanted to make a program to keep track of which numbers from 0 to 9 a user has typed in. You could create an array of size 10, and set the corresponding array index for each number when the number is typed in. So for example, if the user has typed in 3, 7, and 9, then only the array indexes at 3, 7 and 9 will be filled and the array will be partially filled.
+
Other reasons why partially filled arrays occur include cases where array indexes are only filled with values once certain conditions are met. Whenever there are filled indexes mixed with empty indexes, the array will be partially filled. For example, say you wanted to make a program to keep track of which numbers from 0 to 9 a user has typed in. You could create an array of size 10, and set the corresponding array index to filled for each number that is typed in (e.g. if the user types in 2, array index at 2 is set to filled). If the user has typed in 3, 7, and 9, then only the array indexes at 3, 7 and 9 will be filled and the array will be partially filled.
  
 
==What a Partially Filled Array Looks Like==
 
==What a Partially Filled Array Looks Like==
Line 22: Line 22:
 
2. How to keep track of the number of filled indexes in the array.
 
2. How to keep track of the number of filled indexes in the array.
  
 +
===Defining a Empty Index===
  
===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 you create a variable but do not assign it a value. 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.
  
The types of arrays you need to know about right now are arrays of string variables, and arrays of primitive variables (int, double, char, boolean).
+
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 an empty index in an array of ints or doubles can usually lead to problems.
 
+
Recall that with string variables, a string can either be set to null, or to the empty string:
+
  
 +
For example, suppose you have a program which stores an assignment mark out of 10 for a group of students. Each student has an index in an array of ints which stores their mark. If you used 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.
  
  

Revision as of 19:44, 5 December 2007

COMP 1010 Home > More With Arrays > Working with Partially Filled Arrays


Introduction

When working with arrays, you will come across situations where some array indexes do not have useful values. These arrays are said to be 'partially filled'. This section will tell you why partially filled arrays occur, what a partially filled array looks like, and how to handle partially filled arrays.

   

{{{Body}}}

Why Partially Filled Arrays Occur

Partially filled arrays occur because once an array size it set, the array size cannot be changed. Therefore, we generally pick an array size that is large enough to hold the potential number of values that need to be stored. For example, if you usually need to store 20 values in an array, but sometimes you might need to store 40, then you need to make an array of size 40. However, this means that on average, half the array indexes will be empty.

Other reasons why partially filled arrays occur include cases where array indexes are only filled with values once certain conditions are met. Whenever there are filled indexes mixed with empty indexes, the array will be partially filled. For example, say you wanted to make a program to keep track of which numbers from 0 to 9 a user has typed in. You could create an array of size 10, and set the corresponding array index to filled for each number that is typed in (e.g. if the user types in 2, array index at 2 is set to filled). If the user has typed in 3, 7, and 9, then only the array indexes at 3, 7 and 9 will be filled and the array will be partially filled.

What a Partially Filled Array Looks Like

Figure 1 below shows the most complex type of filled index layout for a partially filled array. While there are other ways to fill the indexes which would still make the array a partially filled array, Figure 1 represents the most general case you will need to handle.

//picture haer


Handling Partially Filled Arrays

There are a few different ways to handle partially filled arrays depending on the layout of the filled indexes. However, given that you will need to handle the most general case (see Figure 1) at some point anyway, that is the only case you need to know how to solve. Thankfully, handling the general case is not that difficult, but it does require knowledge of two things:

1. How you want/need to define an array index to be empty, based on the type of variable that gets stored in the array.

2. How to keep track of the number of filled indexes in the array.

Defining a Empty Index

The obvious value to choose when determining if an index is empty is the default value that a variable is assigned when you create a variable but do not assign it a value. 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 an 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. Each student has an index in an array of ints which stores their mark. If you used 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.



more text after the wicked boxenz

Checking for Empty Array Values

wahooo!

Example.jpg