Reputation: 2745
Here it says:
Arrays are useful mostly because the element indices can be computed at run time. Among other things, this feature allows a single iterative statement to process arbitrarily many elements of an array. For that reason, the elements of an array data structure are required to have the same size and should use the same data representation.
Is this still true for modern languages?
For example, Java, you can have an array of Objects or Strings, right? Each object or string can have different length. Do I misunderstand the above quote, or languages like Java implements Array differently? How?
Upvotes: 0
Views: 83
Reputation: 2558
In java all types except primitives are referenced types meaning they are a pointer to some memory location manipulated by JVM
.
But there are mainly two types of programming languages, fixed-typed
like Java
and C++
and dynamically-typed
like python
and PHP
. In fixed-typed
languages your array should consist of the same types whether String
, Object
or ...
but in dynamically-typed
ones there's a bit more abstraction and you can have different data types in array (I don't know the actual implementation though).
Upvotes: 1
Reputation: 6404
An array is a regular arrangement of data in memory. Think of an array of soldiers, all in a line, with exactly equal spacing between each man.
So they can be indexed by lookup from a base address. But all items have to be the same size. So if they are not, you store pointers or references to make them the same size. All languages use that underlying structure, except for what are sometimes called "associative arrays", indexed by key (strings usually), where you have what is called a hash table. Essentially the hash function converts the key into an array index, with a fix-up to resolve collisions.
Upvotes: 1