What is an “Array”?
An array is a variable data type that stores a collection of values referenced by an index number.
For example, suppose we wished to store the names of the 12 months of the year: January, February, March, … , November, December so we can convert a month number to a text name. Month 1 converts to “January”, Month 2 converts to “February” and Month 12 converts to “December”.
An easy way to do this conversion is to set up an array that, in concept, looks something like this:
Month = “January”
Month = “February”
Month = “March”
Month = “April”
Month = “May”
Month = “June”
Month = “July”
Month = “August”
Month = “September”
Month = “October”
Month = “November”
Month = “December”
Month is the name of this variable – but unlike other variables, this one stores 12 separate values. Each value is referenced by using an index – if the index value is 2, then Month[index] refers to “February”.
If we have a date, such as (US-style) – 2/14/15, meaning February 15, 2015, we can convert the month number of 2, into the text month name by referencing Month.
If you have studied algebra math or beyond, you have encountered array variables such as X1, X2, X3, … Xn where the value Xi is read as “X subscript i”. This is the same concept as an array of values. If you have not studied algebra, this notation may be new to you.
Most programming languages support an array variable type but App Inventor does not support arrays. Yet arrays are a convenient way of storing and working with some types of data. To support an array-like variable in App Inventor, we can use a list and map the array subscript to an index position in the list. By hiding this within a couple of procedures, we can simulate the array variable type.
Sample User Interface
I created a simple app to demonstrate the use and implementation of the array. You can download this app from the MIT App Inventor Gallery here.
Continue reading Implementing an “Array” in App Inventor