简体   繁体   English

为什么Array元素必须属于同一类型

[英]Why does Array elements have to be of the same type

Array DS requires all its members to have the same time. Array DS要求其所有成员具有相同的时间。 Java throws ArrayStoreException when an attempt has been made to store the wrong type of object into an array of objects. 当尝试将错误类型的对象存储到对象数组中时,Java会抛出ArrayStoreException Don't remember what C++ does. 不记得C++作用。

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. 如果物体具有不同的类型并因此具有不同的尺寸,则上述公式将不起作用。

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. 我的意思是:使用Java语言的人(可能与C ++相同)正在使用静态类型语言。

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. 警告:在Java中,一切都是一个Object,所以你总是可以声明一个Object []并将任何东西都填入其中。 Strings, Numbers, whatever. 字符串,数字,等等。

And that also leads to the other important aspect: in C++, your array represents an area in memory. 这也导致了另一个重要方面:在C ++中,您的数组代表内存中的一个区域。 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. 另一方面,在Java中,数组指向原始内存。

Long story short: there are real differences between Java and C++ in this context (that one has to understand to make an informed decision); 简而言之:在这种情况下,Java和C ++之间存在着真正的差异(人们必须明白这一点才能做出明智的决定); 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 . 如果你想要一组无关的东西,我宁愿称之为元组

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. 此外,如果要在单个数组中存储不同的数据类型,则可以拥有一个void指针数组。 For instance in c++, do 例如在c ++中,做

void * a[N]

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

Similarly, use Object[] in java. 同样,在java中使用Object[]

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 (eg x[i] ), default initialisation of the elements, dealing with alignment restrictions, ...). C ++语言(和编译器)需要元素的类型出于各种原因存储在数组中,如指针算术和数组预订(例如x[i] ),元素的默认初始化,处理对齐限制,... )。

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. 但是还有其他原因,其他问题如对齐,数组到指针衰减,默认初始化逻辑也可能更微妙但也很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM