Difference between revisions of "Working with Partially Filled Arrays"
Christopher (Talk | contribs) |
Christopher (Talk | contribs) |
||
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 | + | {{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.}} |
==Why Partially Filled Arrays Occur== | ==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 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. | |
− | + | ==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 an Empty Index=== | |
− | + | 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). | |
− | + | Recall that with string variables, a string can either be set to null, or to the empty string: | |
− | |||
− | |||
Revision as of 18:27, 5 December 2007
COMP 1010 Home > More With Arrays > Working with Partially Filled Arrays
IntroductionWhen 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 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.
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 an Empty Index
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).
Recall that with string variables, a string can either be set to null, or to the empty string:
more text after the wicked boxenz
Checking for Empty Array Values
wahooo!