Arrays¶
Time Complexity |
Space Complexity |
||
Access |
Search |
Insert / Delete |
O(n) |
O(1) |
O(n) |
O(n) |
|
Note
This page goes over standard arrays. Typically these are only used (in C++, C#, and Java) for performance reasons. For the preferred arrays you should instead be using, see Preferred Arrays. It’s still important to know how to use these if they appear.
An “array” is just a group of variables. They make organization easier.
For example, you could have twelve strings for months like "January", "February", "March",
and so on, all assigned to similarly named string variables. Or, you could have an array, which would look something
like months[index], where months is the array name, and index is the specific value in the
array you’re accessing. In that example, months[0] would refer to "January" and months[1]
would refer to "February", up to months[11] which would refer to "December".
You’ll notice the index 0 refers to the first month, while the index 11 refers to the twelfth month.
This is because arrays begin at 0, and so everything is off by one, an important detail to remember.
Below are all the important aspects to using an array, chiefly reading and manipulating values. There are other aspects, but these are the most used.
#include <iostream>
using namespace std;
int main(){
// Declaration
int array[5] = {2, 2, 3, 4, 5};
// Change values
array[0] = 1;
// Get values
cout << "first spot: " << array[0] << endl;
// Get size
int size = sizeof(array) / sizeof(array[0]);
cout << "size: " << size << endl;
// Accessing all values
for(int i = 0; i < size; i++)
cout << "index: " << i << " value: " << array[i] << endl;
// or
for(auto number : array) // C++ 11
cout << "value: " << number << endl;
return 0;
}
class Program {
static void Main(string[] args)
{
// Declaration
int[] arrayEx = new int[5];
// or
int[] arrayEx2 = new int[] {2, 2, 3, 4, 5};
// or
int[] array = {2, 2, 3, 4, 5};
// Change values
array[0] = 1;
// Get values
System.Console.WriteLine("first spot: " + array[0]);
// Get size
System.Console.WriteLine("size: " + array.Length);
// Accessing all values
for(int i = 0; i < array.Length; i++)
System.Console.WriteLine("index: " + i + " value: " + array[i]);
// or
foreach (int number in array)
System.Console.WriteLine("value: " + number);
}
}
public class arrays {
public static void main(String[] args) {
// Declaration
int[] arrayEx = new int[5];
// or
int[] arrayEx2 = new int[] {2, 2, 3, 4, 5};
// or
int[] array = {2, 2, 3, 4, 5};
// Change values
array[0] = 1;
// Get values
System.out.println("first spot: " + array[0]);
// Get size
System.out.println("size: " + array.length);
// Accessing all values
for(int i = 0; i < array.length; i++)
System.out.println("index: " + i + " value: " + array[i]);
// or
for(int number : array)
System.out.println("value: " + number);
}
}
// Declaration
var arrayEx = [];
// or
var array = [2, 2, 3, 4, 5];
// Change values
array[0] = 1;
// Add value to end
array.push(6);
// Remove last value
array.pop();
// Get values
console.log("first spot: " + array[0]);
// Get size
console.log("size: " + array.length);
// Accessing all values
for(var i = 0; i < array.length; i++)
console.log("index: " + i + " value: " + array[i]);
// or
array.forEach(function(number) {
console.log("value: " + number);
});
# Declaration
arrayEx = []
# or
array = [2, 2, 3, 4, 5]
# Change value
array[0] = 1
# Add value to end
array.append(6)
# Remove last value
array.pop()
# Get values
print("first value:", array[0])
# Get size
print("size:", len(array))
# Accessing all values
for index in range(len(array)):
print("index:", index, "value:", array[index])
# or
for number in array:
print("value:", number)
// Declaration
var arrayEx = [Int]()
// or
var array = [2, 2, 3, 4, 5]
// Change value
array[0] = 1
// Add value to end
array += [6] // or array = array + [6]
// Remove last value
array.removeLast()
// Get values
print("first value: \(array[0])")
// Get size
print(array.count)
// Accessing all values
for index in 0..<array.count {
print("index: \(index) value: \(array[index])")
}
// or
for number in array{
print("value: \(number)")
}
Notes¶
Arrays in C++ are “static” by default. This means that an array with a size of five things can’t be any bigger or smaller after it is made. This also means that you need to declare how big the array is in code, with an unchanging number. This is the main reason “Vectors” are used instead in C++ for most purposes (besides performance), because it can be resized. See the page Preferred Arrays for how to use those.
The line sizeof(array) / sizeof(array[0]); is a standard way of getting the size of the array, because
that information isn’t stored. What it’s doing here is dividing the size of the array, in bytes, with the size
of the array type (first item of array) in bytes. This results in the number of items in the array.
The C++ 11 line is a “range-based for loop”, the loop lets you access each variable without using an index.
Note
There is one line marked as C++ 11 here, so add “-std=c++11” after “g++” when compiling if it doesn’t work.
If you’re stuck with an older version of C++, use the “for” loop directly above it instead:
for(int i = 0; i < size; i++)cout << "index: " << i << " value: " << array[i] << endl;A neat trick also in C++ 11 is to initialize an array with all “0” values, like this:
int array[100] = {0};Arrays in C# are “static” by default. This means that an array with a size of five things can’t be any bigger or smaller after it is made. This also means that you need to declare how big the array is in code, with an unchanging number. This is the main reason “Lists” are used instead in C# for most purposes (besides performance), because it can be resized. See the page Preferred Arrays for how to use those.
There are multiple ways to declare arrays; any of the above should work. When giving starting values,
it automatically gives the array the size of the number of values, however when starting with no values,
a number is needed in the square brackets. Default values are mandated in C# for standard types; for int
types, 0 is the default in a new array.
After the normal “for” loop there is a “range-based for loop”, the loop lets you access each variable without using an index.
Arrays in Java are “static” by default. This means that an array with a size of five things can’t be any bigger or smaller after it is made. This also means that you need to declare how big the array is in code, with an unchanging number. This is the main reason “List” types such as “ArrayList” are used instead in Java for most purposes (besides performance), because it can be resized. See the page Preferred Arrays for how to use those.
There are multiple ways to declare arrays; any of the above should work. When giving starting values,
it automatically gives the array the size of the number of values, however when starting with no values,
a number is needed in the square brackets. Default values are mandated in Java for primitive types; for int
types, 0 is the default in a new array.
After the normal “for” loop there is a “range-based for loop”, the loop lets you access each variable without using an index.
You’ll notice this is the same as on the Preferred Arrays page. This is because there is no “alternative” array type, and this sort of array can do everything already. Neat.
Usage should be self explanatory. It’s relatively simple in this language.
After the normal “for” loop there is a “range-based for loop”, the loop lets you access each variable
without using an index. In JavaScript you put in your own function as a paramater of the forEach function.
Note
There are no default values in a JavaScript array. In fact, an initialized array given a size won’t have
any indices to begin with, and instead effectively be an empty array. Ex. var array = new Array(3); logs
as [].
You’ll notice this is the same as on the Preferred Arrays page. This is because there is no “alternative” array type, and this sort of array can do everything already. Neat.
After the normal “for” loop there is a “range-based for loop”, the loop lets you access each variable without using an index.
You’ll notice this is the same as on the Preferred Arrays page. This is because there is no “alternative” array type, and this sort of array can do everything already. Neat.
To insert a variable into a string to do things like log variables, the syntax words \(yourVar) more words is
used, where yourVar is your variable and the entire string is enclosed in quotation marks.
There are only “range-based for loops” in swift. To use it like a normal for loop, give it a range with the syntax
0..<number in place of the array, where 0 is the starting index and number is the index
it stops short of. ..< means it stops short of the number and ... means it stops at the number.