3 ms·
> Which method removes and returns the last element of an array? The answer is “pop”, right? I think that’s a fairly generic data structures question (if you t
by relaxing 2y ago
> Which method removes and returns the last element of an array?
The answer is “pop”, right? I think that’s a fairly generic data structures question (if you treat the word array as a standin for any modern iterable collection type.)
- layer8 2y agoStacks in computer science aren’t random-access data structures, which arrays are. Conversely, arrays in computer science are by default thought of as having a fixed size, and don’t inherently have a dynamic size variable associated with them that would be necessary for a pop operation. You can implement a stack on top of an array, but that doesn’t mean that they are the same data structure. The array would be an implementation detail of the stack. (Alternatively, a stack can be implemented as a linked list.)
- relaxing 2y ago40 years ago that would have been true, but meaning has since shifted. Hence the last statement in my response, which you ignored.
- layer8 2y agoThat's the usage in JavaScript, but there has been no general shift. What I wrote is true in Java, Go, Rust, and Python, for example. In theoretical computer science, it is certainly the default assumption when the word "array" is used without qualification. See also https://en.wikiversity.org/wiki/Data_Structures_and_Algorithms/Arrays,_Lists_and_Vectors https://en.wikiversity.org/wiki/Data_Structures_and_Algorith... Or see the use of arrays in "Introduction to Algorithms", one of the standard textbooks about algorithms and data structures, the current edition being from 2022. There is no notion of arrays being resizable there.
- rafram 2y agoBut not PHP (where an array is actually a dictionary, too...), Perl, Swift, or most scientific languages like Matlab and R. C-influenced languages usually differentiate between "arrays" and "lists"/"vectors", but that's just convention. Java, of course, calls its standard general-purpose list implementation both "array" and "list."
- reshlo 2y ago> C-influenced languages usually differentiate between “arrays” and “lists”/“vectors” On that note, Swift is in the process of introducing a fixed-size array type which they intend to call “Vector”.
- layer8 2y agoJava doesn’t call any of its List implementations “array”. There’s ArrayList, which just means a list implemented on top of an array (actually multiple arrays in the general case, since it has to switch between arrays when increasing capacity). It doesn’t mean that it is an array, any more than a stack implemented on top of an array is an array (it isn’t).