If I create an array that can hold 10 objects, is it possible for that array to less then 10 objects?

huangapple go评论60阅读模式
英文:

If I create an array that can hold 10 objects, is it possible for that array to less then 10 objects?

问题

我需要创建一个能够容纳10个对象的数组。如果我创建这个数组,它能够容纳少于10个对象吗?例如,数组中有7个对象,还有3个空位。这种情况可能吗?

英文:

I need to create an array that can hold 10 objects. If I make this array will it be able to hold less then the 10 object. An example would be the array has 7 objects and room for 3 more. Is this possible?

答案1

得分: 1

是的。在某些索引处赋值为null。此外,您可以创建一个数组,其中不存储任何对象,只需指定长度。

英文:

Yes. Assign null at some indices. Also, you can create the array without any objects stored in it, specifying the length.

答案2

得分: 1

数组是一种数据结构,用于存储固定大小的、相同类型的元素的顺序集合。当您初始化一个数组时,它最多可以具有数组大小的长度,因此对于您的答案来说,它可以有空单元。

英文:

Array is a data structure which stores a fixed-size sequential collection of elements of the same type. When you initialize an array, it can have at most the till the size of array, so for your answer it can have empty cells.

答案3

得分: 1

是的。数组可以容纳任意数量的项目,但不包括您为其分配的长度。在Java中,原始类型的数组(int、bool、double、char等)将在任何非初始化器列表数组中填充该类型的默认值,对于任何对象类型的数组,将填充空引用。

然而,像您这样的问题更适合课堂讨论,因为您可能仍在学习计算机科学的材料,看起来是这样的。

英文:

Yes. Arrays can hold any number of items up to, but not including the length you have allocated for it. In Java, arrays of primitive types (int, bool, double, char, etc.) will be filled with the default value of that type for any non-initalizer-list array, and null references for any arrays of Object types.

However, questions like yours are more suited for classroom discussion, as you may still be learning computer science material, it seems like.

答案4

得分: 1

这取决于你的意思。

所有的数组都有固定的长度,每个数组始终包含的元素数量正好等于它的长度。从这个意义上说,数组不能有多余的容量;它们总是完全填满的。然而,你可以在外部跟踪哪些元素包含有效数据,并忽略其他元素。

严格来说,没有数组包含对象,但许多数组包含引用(各种类型的引用,与原始值相对)。我们在用语言上有点随意,通常称这些为对象数组,这是我对这个问题的解释。然而,当我们意识到数组引用的任何元素可能包含值null(表示不引用任何对象)时,这个区别变得重要。因此,具有一些元素为null的引用数组所指的实际对象比其长度少。你可以把这看作是数组包含的对象少于其长度。

请注意,null元素不局限于数组末尾。它们可以出现在任何索引处,并与非null元素交错出现。

尽管如此,我怀疑你正在寻找List,特别是java.util.ArrayList。列表在许多方面比数组更灵活,包括它们具有可调整的大小。而且,ArrayList确实在其当前容量和当前大小之间有区别,尽管容量会根据需要扩展,而不像数组的长度那样固定。类名反映了它是使用数组实现的,其性能特性也反映了这一点。

英文:

It depends on what you mean.

All arrays have fixed length, and each always contains a number of elements exactly equal to its length. In that sense, no, arrays cannot have excess capacity; they are always completely filled. You can, however, keep track externally of which elements contain valid data, and ignore the others.

Technically, no array contains objects, but many contain references (of various types; as opposed to primitive values). It is conventional to be a bit sloppy with our language by calling those arrays of objects, and that's how I interpret the question. The distinction becomes important, however, when we recognize that any element of an array of references may contain the value null, which does not refer to an object. Thus, an array of references with some elements null refers to fewer actual objects than its length. You might characterize that as the array containing fewer objects than its length.

Note that null elements are not limited to the end of an array. They may appear at any index, interspersed with non-null elements.

With all that said, however, I suspect you're looking for Lists, and specifically java.util.ArrayList. Lists are more flexible than arrays in many ways, including that they have adjustable size. And ArrayList indeed does have a distinction between its current capacity and its current size, though the capacity is expanded as needed, not fixed like an array's length. The class name reflects that it is implemented with use of arrays, and its performance characteristics reflect that.

答案5

得分: 0

>我需要创建一个能够容纳10个对象的数组。如果我创建这个数组,它能容纳少于10个对象吗?一个例子是数组有7个对象,并有3个空位。这是可能的。

简而言之,是的。它将能够容纳0到10个对象。但这并不意味着你的数组可以从0增长到10,或者从10减少到0。它将是一个固定大小为10的数组(无论你是否正在使用它)。

长的回答是,始终会有10个元素填充有一些值(无论你是否正在积极使用所有元素)。那些你认为目前没有在使用的元素仍将填充默认值,根据数组的数据类型(例如,对象的null,整数的0,布尔值的false)。

例如,创建一个大小为10的int数组:

int array[] = new int[10];

默认情况下,您已经有了10个数组(全部填充了默认的0)。

索引: 0  1  2  3  4  5  6  7  8  9
数组: [0][0][0][0][0][0][0][0][0][0]

您将始终拥有这10个元素供您使用。现在取决于您如何使用这10个元素。

如果您正在寻找可以缩小和增长的解决方案。您可以考虑使用java.util.ArrayList,它可以在您从列表中删除/添加项目时收缩/增长。

英文:

>I need to create an array that can hold 10 objects. If I make this array will it be able to hold less then the 10 object. An example would be the array has 7 objects and room for 3 more. Is this possible?

In short, yes. It will be able to hold 0 to 10 objects. However it doesn't mean your array can shrink and grow from 0 to 10. It will be a fixed size of 10 (be it you are using it or not).

The long answer is, there will always be 10 elements populated with some values (be it you are actively using all elements or not). Those elements which you think you are not using at the moment will still be populated with default value according to the data type of the array (i.e. null of object, 0 for integer, false for boolean).

For example, creating an int array of size 10:

int array[] = new int[10];

By default, you will already have 10 arrays (all populated with default 0).

Index:  0  1  2  3  4  5  6  7  8  9
Array: [0][0][0][0][0][0][0][0][0][0]

You will always have these 10 elements at your disposal. It is now up to you how you want use these 10 elements.

If you are looking for something that can shrink and grow. You can look into using java.util.ArrayList which can shrink/grow as you remove/add items into the list.

huangapple
  • 本文由 发表于 2020年9月26日 01:01:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64068443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定