|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
General Uses for Arrays
As discussed in the introduction, an array is used for collecting data
of the same type together into one variable. In this section, we will
discuss some of the cases where an array is an appropriate data
structure to use.
Although arrays are essentially ordered lists of elements, it can still
be appropriate to use an array to store data when the order
of the data does not matter. Consider the example of a class of
students where you need to store data about all of the students. If
this were the problem you had to solve, you may create some data type
that held all of the relevant information about any one particular
student, such as name and average. Then you would use an array to store
a student record for each student in the class. In a case like this,
the index of each student is arbitrary; their position in the array
means nothing. Instead you would be benefitting from the the fact that
you can loop through the entire array and access the information about
each of the students. This might be useful if you were interested in
calculating class-wide statistics, such as what the average grade in
the class was.
Similarly, you can also take advantage of the ordered nature of arrays.
This is often used in cases where you needed to sort data. Reconsider
the above class example. You may want to know the ranking of everyone
in the class. Conveniently, all of the necessary information is already
stored in the array. All you need to do is to apply one of the many
sorting algorithms to the put the array into order by
score.
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contact Us | Privacy Policy | Terms and Conditions | About
©2006 SparkNotes LLC, All Rights Reserved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||