diff --git a/book/content/part02/array.asc b/book/content/part02/array.asc index b301f7cf..5ad54b9e 100644 --- a/book/content/part02/array.asc +++ b/book/content/part02/array.asc @@ -17,7 +17,7 @@ TIP: Strings are a collection of Unicode characters and most of the array concep .Fixed vs. Dynamic Size Arrays **** -Some programming languages have fixed size arrays like Java and C++. Fixed size arrays might be a hassle when your collection gets full, and you have to create a new one with a bigger size. For that, those programming languages also have built-in dynamic arrays: we have `vector` in C++ and `ArrayList` in Java. Dynamic programming languages like JavaScript, Ruby, Python use dynamic arrays by default. +Some programming languages have fixed size arrays like Java and C++. Fixed size arrays might be a hassle when your collection gets full, and you have to create a new one with a bigger size. For that, those programming languages also have built-in dynamic arrays: we have `vector` in C++ and `ArrayList` in Java. Dynamic programming languages, like JavaScript, Ruby, and Python, use dynamic arrays by default. **** Arrays look like this: @@ -29,30 +29,24 @@ Arrays are a sequential collection of elements that can be accessed randomly usi ==== Insertion -Arrays are built-in into most languages. Inserting an element is simple; you can either add them on creation time or after initialization. Below you can find an example for both cases: +Arrays are a built-in part of most languages. In JavaScript, populating an array is simple; elements can either be added at the time of initialization, or after. See below for examples of both cases: .Inserting elements into an array [source, javascript] ---- -// (1) Add elements at the creation time: +// (1) Initialize an array pre-populated with elements: const array = [2, 5, 1, 9, 6, 7]; -// (2) initialize an empty array and add values later +// (2) initialize an empty array and add elements later const array2 = []; array2[3] = 1; array2[100] = 2; -array2 // [empty × 3, 1, empty × 96, 2] ----- - -Using the index, you can replace whatever value you want. Also, you don't have to add items next to each other. The size of the array will dynamically expand to accommodate the data. You can reference values in whatever index you like index 3 or even 100! In the `array2` we inserted 2 numbers, but the length is 101, and there are 99 empty spaces. -[source, javascript] ----- console.log(array2.length); // 101 console.log(array2); // [empty × 3, 1, empty × 96, 2] ---- - +Using the index, you can access and/or modify any value you choose. Also, you can reference values in any index you choose. The size of the array will dynamically expand to accommodate the data. For example, in `array2` 2 numbers have been inserted, but the length is 101, and there are 99 empty spaces. The runtime for inserting elements using index is always is constant: _O(1)_. ===== Inserting at the beginning of the array