简体   繁体   English

有人可以帮我理解 object[] toArray 吗?

[英]Can someone please help me understand object[] toArray?

toArray() -- this method returns a newly allocated array containing the elements in the multiset. toArray()——这个方法返回一个新分配的数组,其中包含多重集中的元素。 The array this method returns must only contain the elements in the multiset and not any nulls or other values that are stored in the backing store, but are not in the multiset.此方法返回的数组必须只包含多重集中的元素,而不是任何空值或存储在后备存储中但不在多重集中的其他值。

considering _store is the array in whicch the elements are stored.考虑到 _store 是存储元素的数组。 I really dont understand the concept of what exactly I'm supposed to do!我真的不明白我应该做什么的概念! I have to complete the method我必须完成方法

Data structures are usually traversed using Iterator , but sometimes, they might be required as arrays.通常使用Iterator遍历数据结构,但有时,它们可能需要作为数组。 So, toArray() method basically provides the contents of the data structure in the form of the array.所以, toArray()方法基本上以数组的形式提供数据结构的内容。 Return type is Object[] so that any object can be returned through it.返回类型是Object[]以便可以通过它返回任何对象。 Explicit type cast is required to obtain the real object.需要显式类型转换才能获得真实对象。

Note: This method must allocate a new array.注意:此方法必须分配一个新数组。 It means that although the contents of the array and data structure are same, but the actual object references are different.这意味着虽然数组和数据结构的内容相同,但实际的对象引用不同。 So that, if someone modified the element in data structure, it does not affect the array.这样,如果有人修改了数据结构中的元素,它不会影响数组。

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

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