Can an array of objects be thought of as a collection of objects in Java?

Oracle Website Definition:

A collection — sometimes called a container — is simply an object that groups multiple 
elements into a single unit.

      

I know Java provides java.util.Collection

. It includes Set

, ArrayList

, Queue

etc.

My question is: Am I wrong if I refer to an array of objects as a collection of objects? (Even if you java.util.Collection

probably didn't include the array)


Edit: I found something interesting. This is how Microsoft defines arrays and collections : http://msdn.microsoft.com/en-sg/library/9ct4ey7x(v=vs.90).aspx

+3


source to share


4 answers


This is a somewhat semantic question. The word "collection" in English can mean "more than one", and thus, yes, an array is a collection. However, in Java, you usually see it written as Collection, with capital C - ie, an object that is subtyped java.util.Collection

. You cannot use an array for this value, since arrays in java do not implement this interface.



+2


source


I don't understand why this is a problem. Literally, an array is a "collection" ( Read ).

But when you dive into Java, the meaning of a collection changes from a simple group of objects to a set of mechanisms for managing (storing, retrieving, working, etc.) objects and is defined by the interface prototype Collection

.



Therefore, unless you override an interface in Java, your array is not exactly a set.

+2


source


An array is an object with a count of a calculus - including 0 - spaces for variables of the same type.
Nothing more. As you wish, you can say that it is some sort of collection, or a list or a sequence.
But all of these terms are well known as names or part of its interfaces or classes, which are much more than a simple array.
So if you use these names for an array, other people may not understand correctly what you mean.
My recommendation: An array is just an array, so call it array

+2


source


One difference that might help you with this dilemma. Array

is an object with contiguous but limited memory. A collection is a collection of objects, thanks to which each object has its own memory address.

Arrays

relatively faster in operations compared to Collections

.

Collections

gives you many useful methods on top Arrays

.

+1


source







All Articles