0% completed
Introduction to Arrays
What Is an Array?
An array is a collection of items (often called elements) stored in a single block of memory. Each item in an array is of the same type (for example, all integers or all strings). Arrays are very common because they allow quick access to elements by using an index.
Key Points
- Same Data Type: All elements are of one type (e.g., int, float, string).
- Index-Based Access: Each element is accessed by an index, usually starting at 0.
- Fixed Position in Memory: Elements are placed in consecutive memory locations, making access by index very fast.
.....
.....
.....
matthew.carnahan1
· 2 years ago
The week 1 material introduces the data structures "Arrays" and "Matrices". I don't recall any specific algorithms were covered though. It seems like when running code that exploits an array or matrix, the main takeaways are:
- Write the code in a way that minimizes the amount of times it has to loop over the array/matrix.
- Write the code in a way that minimizes (and ideally eliminates) duplicate operations. For example, when building an array with cumulative sums, calculate each value as the previous index's cumulative sum, plus the next value.
Does everyone concur with this? Are there any other takeaways for writing code that uses arrays/matrices? Were there specific algorithms that I missed?
Iaroslav L
· 2 years ago
In the Array introduction section we have the following piece of code, saying that staticArray has fixed size:
from array import array # Static Array (Using the array module for strictly typed arrays; # however, lists are more commonly used and can be considered dynamic.) staticArray = array('i', [0]*5) # Array of integers with a fixed size of 5 staticArray[0] = 1
That's actually not true. Because it's still possible to append an item there:
staticArray.append(10) print(staticArray) # output: array('i', [1, 0, 0, 0, 0, 10])
Sara Kim
· 6 months ago
- Same Data Type: All elements are of one type (e.g., int, float, string).
I thought the elements in an array don't necessarily have the same type. It depends on the programming language. For example, Python and Javascript allow you to have different types of elements in an array.