Skip to content

Commit e54b65b

Browse files
Array Readme text file - shrinivaskane (#124)
Co-authored-by: Ryan Maleki <spring1843@users.noreply.github.com>
1 parent 971735e commit e54b65b

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

array/README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
# Array
22

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.
44

55
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.
66

77
## Implementation
88

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.
1010

1111
```Go
1212
package main
1313

1414
import "fmt"
1515

1616
func main() {
17+
// Declare an array with 2 int elements, defaulting to 0.
1718
var nums1 [2]int
19+
// Initialize an array with 3 int elements.
1820
nums2 := [3]int{1, 2, 3}
21+
// Print both arrays.
1922
fmt.Println(nums1, nums2) // Prints [0 0] [1 2 3]
2023
}
2124
```
2225

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.
2427

2528
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.
2629

@@ -30,10 +33,11 @@ package main
3033
import "fmt"
3134

3235
func main() {
36+
// Create a slice with elements 1, 2, 3.
3337
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
3741
fmt.Println(nums) // Prints [0 1 2 3]
3842
}
3943
```
@@ -60,12 +64,15 @@ package main
6064
import "fmt"
6165

6266
func main() {
67+
// Initialize a slice with 6 elements.
6368
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
75+
// Print final slice.
6976
fmt.Println(nums) // Prints [4]
7077
}
7178
```

0 commit comments

Comments
 (0)