Teach Any Computer Science Class
We have put together a full GCSE Computer Science curriculum that will give you all the teaching materials you need to teach any topic.
Whether you're a brand new Computer Science teacher, or you've been teaching ICT for years, our resources will save you hours and hours of lesson preparation every single week.View the Lessons →
You can create a variable as follows:
Var num = 3;
This means that you have a variable with a value of 3.
Or, you can create a variable as follows, where the value can change:
Var nums = [3,7,9,10];
In the example above, nums is an array.
What is an Array?
An array is a list of values separated by commas embedded inside open and close brackets. The order of an array is important because each element in an array has an index.
An index refers to the sequence where there is a first element, second element, last element, etc. The ability to have the elements in order is an important characteristic of an array.
The count of an index starts at 0. For 4 elements, there will be index values of 0 to 3, like in our example above:
Var nums = [3,7,9,10];
Index 0 = 3
Index 1 = 7
Index 2 = 9
Index 3 = 10
nums refer to the whole array, but if you want to refer to the individual elements of an array, you have to use its index.
To call an element of an array, you call its index, like in our example:
where the value will be 9, since you are calling index 2.
You can put strings, text, numbers or objects into an array.
var words = [“sun”, “tree”, “dog”];
You can also declare an array with a data type, array name and array size. For example:
int score ;
This means that we are creating an array called score of an integer data type and an array size of 100.
An array is a list of data. It is the data structure that allows a user to have a list of n number of data. It is useful when the user has a large amount of data to be able to call in just one line of code. There’s no need to list each data multiple times.
A user can do multiple things with an array like sort and execute arithmetic functions like add, subtract, multiply and divide for an array of integers.
Characteristics of an array
- It is homogeneous – meaning all the elements of an array are of the same data type.
- It is a linear data structure – the elements in an array are arranged in a sequential manner.
- It is a static data structure – when you create an array, all of its memory has to be allocated in a contiguous block and its size is fixed.
Types of array
1. Linear array or one-dimensional array (1-D)
- Each element is represented by a single subscript.
- Example: A  means there are 3 elements.
2. Multi-dimensional array
a) Matrix array or two-dimensional array (2-D)
- Each element is represented by two subscripts.
- Example: A   means there are 3 rows and 5 columns, 3*5 = 15 elements.
b) Three-dimensional array
- Each element is represented by three subscripts.
- Example: A    means there are 3*5*2 = 30 elements.
Advantages of an array
- You can access any element in an array directly.
- You can use a single name to represent a large number of data.
- You can use an array to create other complex data structures like stacks and graphs.
Disadvantages of an array
- When you need to add a new element to an array and the array is already full, you have to create a new array and copy to it all the other elements from the previous array.
- Even if the array isn’t full, its memory is already allocated and cannot be used for other purposes.
- Inserting and deleting elements of an array is difficult since they are stored in a contiguous manner.