Arrays Flashcards
Why are arrays better at storing data than variables?
If you have lots of similar data you can store it in 1 array instead of multiple variables as each variable is inefficient.
What is an array?
A data structure that can store a collection of data values under one name.
What is each piece of data in an array called?
an element
How can each element be assessed in an array?
using its position or index
When are arrays most helpful?
When you have lots of related data that you want to store and it doesn’t make sense to use separate values.
e.g. the names of pupils in a class, marks in a test etc
How are they similar to variables?
Some languages require you to declare arrays before you use them.
What is a one-dimensional array like?
lists
How does this array work? array rowers[4] rowers[0] = "Mark" rowers[1] = "Adam" rowers[2] = "Shelly" rowers[3] = "Tobias"
The first line creates the array “rowers” and makes it size 4 (contains only 4 elements). The other lines assign the strings (names) in their positions in the array: 0,1,2 and 3.
How can you retrieve elements from an array?
Use the name of the array and element’s position. Positions are numbered always starting at 0.
How to change the value of the elements?
Reassigning the array position to a different data value. The previous value will be removed completely.
How can FOR loops improve arrays if you combine them?
More systematic way of accessing and changing all of the elements in an array. They can also be used to search for specific elements or make a similar change to lots of elements.
What is a two-dimensional array?
A one-dimensional array where each element is also a one-dimensional array.
How is the position of an element written in a two-dimensional array?
[a,b] or [a][b], where a represents the position of the one-dimensional list that the element is in and b represents its position within that one dimensional list.
An example of a two-dimensional array:
trees = [[“oak”, “ash”], [“beech”, “cedar”], [“pine”, “elm”]]
How do you change elements in a two-dimensional array?
The exact same as in a one-dimensional array.