Arrays
Arrays hold multiple values within one variable name.
Last updated
Arrays hold multiple values within one variable name.
Last updated
Think of an array like a line-up of connected bins, each with its own home address (called an index
). The individual values (or boxes) are called elements
. Let's say you need to store ten integers for a guessing game. You would not want to declare ten individual integer variables. Instead, you could declare one array variable of size 10 to hold the numbers.
You can store or retrieve each number by calling its address (index
), typically using square brackets [ ]
. Javascript shown here, other syntaxes shown in :
Note: A string
is actually an array of char
variables.
Since Javascript does not have types like C++, we just make an array of variables. Javascript also has some great built-in methods that allow us to concatenate, join, search, and sort arrays (among other things). Note - in Javascript there is no need to declare empty arrays with an initial size.
You can retrieve the information of a single element by calling the index:
x[5]; // Retrieve the element at index 5 (the 6th element)
In this same manner, you can loop through, or iterate
through all the elements in an array.
This example prints the elements of an array. We could
Notice the use of someIntegers.length
on line 5, above. In many languages you can retrieve the number of elements in this or similar ways. It is crucial to understand, however, that the value representing length is 1 higher than the last index in the array. For example, the following array has 8 elements but the last index is 7 because indexes start at zero.
Adding and removing elements from an array is memory-intensive. It is quite a process at the lowest-level, involving new memory space if growing your array and copying elements over. It can really slow down the program if doing this many thousands of times for large amounts of data.
In Javascript, adding an element to the end of an array is very simple - you use the push()
function.
someArray.push(5) // Add integer 5 onto the end of the array
someArray.push(7, 3, -9) // You can push multiple items in one command
The push()
function returns a number representing the new length of the array.
The splice
function takes the parameters of where to insert the new item, how many items to delete (in this case, zero), and the item to insert. You can insert many items in one command.
Removing the last element from the end of an array is done using the pop()
function.
someArray.pop() // Remove the last element
The pop()
function returns the element it is removing.
Deleting elements within an array also uses the splice()
function, similar to adding elements. The parameters you would use are the location and number of elements to remove:
someArray.splice(5, 4) // Starting at index 5 remove 4 elements
Again, it will return a value representing the new length of the array.
Modifying the values in-place within an array for a strongly-typed language is much less memory intensive since each index simply points to a location in memory. Loosely-typed languages, like Javascript and Python, require much more memory-intensive work to modify an element, especially if changing the type (for example, swapping an integer for a large string of text).
Javascript:
In programming, a string
is actually an array of chars
. Well, in reality a string
is an object
but I won't get into that here.
The string "Hello" is actually a char
array of size 5 where char[0] = 'H'
, char[1] = 'e'
, char[2] = 'l'
, etc.
Most languages allow you to manipulate a string using the indexes of the characters. Also, when you append to a string or concatenate multiple strings together the computer must allocate a new chunk of memory that is the specific size of the new char array and individually assign each index the appropriate letter.
Simple arrays, as discussed in these sections, are called one dimensional arrays. They are linear in the sense that there is a beginning & end, and the indexes in-between have an element on either side, like links in a chain.
Arrays can be two dimensional as well (or even more). A 2D array has two indexes, [x, y]
, such that each element in the first index is, itself, an array.
You can also chain arrays into a 3D array. The idea there is that each element points to a 2D array of arrays. You can create all sorts of amazing combinations in this fashion.
Lots more reading and .
Python has a more versatile 'array' type called a sequence
. The most basic sequence is a . Lists in Python act very similar to arrays in Javascript. They can be modified, contain any type of variable or object, and they have many built-in methods for utilization.
Aside from , Python also has Tuples
and Dictionaries
. Read more about all of .
Iterating is the process of going through each (or every ) element in an array.
As with Javascript and other languages, you can use a loop or other type of loop to go through the elements of an array. The one large difference is the lack of a .length
property. You must either know the length of the array or use the size of memory space to find the length... yuck!
Python's lists
(or sequences
as I'm known to call them) can be referenced, retrieved, and modified in many interesting ways. If you are interested in Python, I highly recommend you read instead of mine!
If you want to insert a new element inside an existing array (or at the front), you need to utilize the function. It is defined as myArray.splice(insertAt, 0, itemToInsert);
Highly recommended you read the page on . Essentially, Python has the following methods (functions) for lists
: