Preferred Arrays¶
Time Complexity |
Space Complexity |
||
Access |
Search |
Insert / Delete |
O(n) |
O(1) |
O(n) |
O(n) |
|
Note
This page goes over improved array types. These are used in place of standard arrays (in C++, C#, and Java) for improved usability in general. For standard arrays with less features but better performance (negligible in light use cases), see Arrays. These are important to know.
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 the “preferred” array types. This means that instead of using the languages native array syntax (“wording”), some sort of internal library is used instead. These add additional functionality to arrays that make them resizeable and have properties storing size, among other things. This additional functionality is often necessary in many use cases, where the standard “static” (unchanging) arrays would be difficult to use at best.
In C++ the preferred array type is a “vector”, in C# a “List”, and in Java an “ArrayList”. other languages are unaffected (standard arrays are practical / not “static”).
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>
#include <vector>
using namespace std;
int main(){
// Declaration
vector<int> array = {2, 2, 3, 4, 5};
// Change values
array[0] = 1;
// Add value to end
array.push_back(6);
// Remove last value
array.pop_back();
// Get values
cout << "first spot: " << array[0] << endl;
// Get size
cout << "size: " << array.size() << endl;
// Accessing all values
for(int i = 0; i < array.size(); i++)
cout << "index: " << i << " value: " << array[i] << endl;
// or
for(auto number : array) // C++ 11
cout << "value: " << number << endl;
return 0;
}
using System.Collections.Generic;
class Program {
static void Main(string[] args)
{
// Declaration
List<int> arrayEx = new List<int>();
// or
List<int> array = new List<int>{2, 2, 3, 4, 5};
// Change values
array[0] = 1;
// Add value to end
array.Add(6);
// Remove last value
array.Remove(array[array.Count-1]);
// Get values
System.Console.WriteLine("first spot: " + array[0]);
// Get size
System.Console.WriteLine("size: " + array.Count);
// Accessing all values
for(int i = 0; i < array.Count; i++)
System.Console.WriteLine("index: " + i + " value: " + array[i]);
// or
foreach (int number in array)
System.Console.WriteLine("value: " + number);
}
}
import java.util.*;
public class preferredArrays {
public static void main(String[] args) {
// Declaration
List<Integer> arrayEx = new ArrayList<Integer>();
// or
List<Integer> arrayEx2 = new ArrayList<Integer>(Arrays.asList(2, 2, 3, 4, 5));
// or
List<Integer> arrayEx3 = new ArrayList<>(Arrays.asList(2, 2, 3, 4, 5));
// or
var array = new ArrayList<>(Arrays.asList(2,2,3,4,5));
// Change values
// .set(index, element)
array.set(0, 1);
// Add value to end
array.add(6);
// Remove last value
array.remove(array.size()-1);
// Get values
System.out.println("first spot: " + array.get(0));
// Get size
System.out.println("size: " + array.size());
// Accessing all values
for(int i = 0; i < array.size(); i++)
System.out.println("index: " + i + " value: " + array.get(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¶
The line #include <vector> is needed to use vectors.
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 < array.size(); i++)cout << "index: " << i << " value: " << array[i] << endl;The line using System.Collections.Generic; is needed to use Lists.
There is no method to remove the last item in a list specifically, so the line
array.Remove(array[array.Count-1]); is used. array.Remove(item) removes an item from a list, and
array[array.count-1] refers to the last item in the list.
After the normal “for” loop there is a “range-based for loop”, the loop lets you access each variable without using an index.
There are multiple ways to declare ArrayLists; any of the above should work.
There is no method to remove the last item in an ArrayList specifically, so the line
array.remove(array.size()-1); is used. array.Remove(index) removes an item at an index, and
array.size()-1 refers to the last index.
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 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. 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 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 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.