Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Array elements have to be of the same type

Array DS requires all its members to have the same time. Java throws ArrayStoreException when an attempt has been made to store the wrong type of object into an array of objects. Don't remember what C++ does.

Is my understanding correct that it's important to have all objects of the same type in array because it guarantees constant time element access through the following two operations:

1) element size * element index = offset
2) array pointer address + offset

If objects are of different types and consequently different size, the above mentioned formula won't work.

like image 660
Max Koretskyi Avatar asked Apr 21 '17 08:04

Max Koretskyi


3 Answers

Because: we want it like this.

What I mean is: people using the Java language (probably the same for C++) are using a statically typed language for a purpose.

And when such people starting thinking in plurals; they typically think in plurals of "similar" things.

Caveat: in Java, everything is an Object, so you can always declare an Object[] and stuff anything into that. Strings, Numbers, whatever.

And that also leads to the other important aspect: in C++, your array represents an area in memory. And you better have same sized elements in that area; to avoid data corruption.

In Java on the other hand, an array is not pointing to raw memory.

Long story short: there are real differences between Java and C++ in this context (that one has to understand to make an informed decision); and then there is the "language" thing itself. In other words: this is not Ruby land, where you just put ducks, numbers, plants and quack sounds in the same "list" without further thinking.

Final thought, based on that joke in the last paragraph: in my eyes, an array is an implementation of the list concepts, thus it is about a collection of things of the same nature. If you want a collection of unrelated things, I would rather call that a tuple.

like image 88
GhostCat Avatar answered Nov 05 '22 21:11

GhostCat


Yes, you are right. All that is required for constant time random access.

Also, you can have an array of void pointers if you want to store different data types in a single array. For instance in c++, do

void * a[N]

a[i] = (void *)(&YourClass)

Similarly, use Object[] in java.

like image 29
v78 Avatar answered Nov 05 '22 23:11

v78


The C++ language (and compiler) requires the type of the elements to be stored in an array for various reasons, like pointer arithmetics and array subscription (e.g. x[i]), default initialisation of the elements, dealing with alignment restrictions, ...).

int x[3] = { 1,2,3 };  // array of 3 int values, each being properly aligned concerning processor architecture;
myObjectType objs[10]; // array of 10 objects of type myObjectType, each being default initialised (probably the default constructor), each being properly aligned
myObjectType *objs[10]; // array of 10 pointers to objects of type myObjectType (including subclasses of myObjectType; allowing dynamic binding and polymorphism). Note: all pointers have the same size, the object to which they point may differ insize.

int *intptr = x;
bool isEqual= (intptr[2] == x[2]); // gives true
intptr += 2; // increases the pointer by "2*sizeof(int)" bytes.

So, yes, you are right: one reason is because of calculating offsets; But there are other reasons as well, and other issues like alignment, array to pointer decay, default initialisation logic are probably more subtle but essential, too.

like image 2
Stephan Lechner Avatar answered Nov 05 '22 21:11

Stephan Lechner