You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: array/README.md
+18-11Lines changed: 18 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,29 @@
1
1
# Array
2
2
3
-
Arrays are a basic and essential data structure in computer science. They consist of a fixed-size contiguous memory block and offer O(1) read and write time complexity. As a fundamental element of programming languages, arrays come built-in in their core.
3
+
Arrays are a fundamental and essential data structure in computer science. They consist of a fixed-size contiguous memory block and offer O(1) read and write time complexity. As a fundamental element of programming languages, arrays come built-in with their core.
4
4
5
5
To provide a real-world analogy, consider an array of athletes preparing for a sprinting match. Each athlete occupies a specific position within the array, typically denoted as 1, 2,…, n. While it is technically possible for each athlete to be in a different position, the positions generally carry some form of significance, such as alphabetical order or seniority within the sport.
6
6
7
7
## Implementation
8
8
9
-
In the Go programming language, arrays are considered values rather than pointers and represent the entirety of the array. Whenever an array is passed to a function, a copy is created, resulting in additional memory usage. To avoid this it is possible to pass a pointer to an array, or use slices instead. The size of the array is constant and it must be known at compile time, and there is no need to use the built-in `make` function when defining arrays.
9
+
In the Go programming language, arrays are considered values rather than pointers and represent the entirety of the array. Whenever an array is passed to a function, a copy is created, resulting in additional memory usage. To avoid this, it is possible to pass a pointer to an array, or use slices instead. The size of the array is constant and it must be known at compile time, and there is no need to use the built-in `make` function when defining arrays.
10
10
11
11
```Go
12
12
package main
13
13
14
14
import"fmt"
15
15
16
16
funcmain() {
17
+
// Declare an array with 2 int elements, defaulting to 0.
17
18
varnums1 [2]int
19
+
// Initialize an array with 3 int elements.
18
20
nums2:= [3]int{1, 2, 3}
21
+
// Print both arrays.
19
22
fmt.Println(nums1, nums2) // Prints [0 0] [1 2 3]
20
23
}
21
24
```
22
25
23
-
Although arrays are fundamental data structures in Go, their constant size can make them inflexible and difficult to use in situations where a variable size is required. To address this issue, Go provides [slices](https://blog.golang.org/slices-intro), an abstraction of arrays that offer more convenient access to sequential data typically stored in arrays. When a slice is passed to a function, the head of the slice is replaced but the slice still points to the same data, hence it is possible for the callee to modify the values of the slice and send them back to the caller.
26
+
Although arrays are fundamental data structures in Go, their constant size can make them inflexible and difficult to use in situations where a variable size is required. To address this issue, Go provides [slices](https://blog.golang.org/slices-intro), an abstraction of arrays that offer more convenient access to sequential data typically stored in arrays. When a slice is passed to a function, only the slice header is passed, but it still references the same underlying array data, hence it is possible for the callee to modify the values of the slice and send them back to the caller.
24
27
25
28
Slices enable adding values using the `append` function, allowing dynamic resizing. Additionally, selectors of the format [low:high] can be used to select or manipulate data in the slice. By utilizing slices instead of arrays, Go programmers gain a more flexible and powerful tool to manage their data structures.
26
29
@@ -30,10 +33,11 @@ package main
30
33
import"fmt"
31
34
32
35
funcmain() {
36
+
// Create a slice with elements 1, 2, 3.
33
37
nums:= []int{1, 2, 3}
34
-
nums = append([]int{0}, nums...) // Add new element to the start
35
-
nums = append(nums, 4) // Add new element to the end
36
-
nums = nums[:len(nums)-1] // Removes last element
38
+
nums = append([]int{0}, nums...) // Add a new element to the start
39
+
nums = append(nums, 4) // Add a new element to the end
40
+
nums = nums[:len(nums)-1] // Removes the last element
37
41
fmt.Println(nums) // Prints [0 1 2 3]
38
42
}
39
43
```
@@ -60,12 +64,15 @@ package main
60
64
import"fmt"
61
65
62
66
funcmain() {
67
+
// Initialize a slice with 6 elements.
63
68
nums:= []int{1, 2, 3, 4, 5, 6}
64
-
nums = nums[:len(nums)-1] // drop last element
65
-
nums = nums[1:] // drop first element
66
-
nums = nums[1:] // all elements from index 1 to the end
67
-
nums = nums[:2] // all elements from index 0 to 1
68
-
nums = nums[1:2] // the element at index 1
69
+
// Sequentially modify the slice.
70
+
nums = nums[:len(nums)-1] // Drop the last element
71
+
nums = nums[1:] // Drop the first element
72
+
nums = nums[1:] // Keep all elements from index 1 to the end
73
+
nums = nums[:2] // Keep all elements up to (but not including) index 2
74
+
nums = nums[1:2] // Keep only the element at index 1
0 commit comments