3

In many programming languages, arrays don't get as much attention as other data structures. In Java, there is no Array<T> type collection to make arrays feel more consistent, such as in inheritance. In Python, there are no arrays, but instead only lists. To have an "array", one has to do something like [0] * size. Arrays aren't obsolete data structures, and it is often more natural to use an array than a list.

Sometimes it makes sense to have a fixed number of elements, some of which may not be set at a point in time. You might imagine a row of cupholders; you can put cups in any position or take them out, but you can't just add another cupholder.

2
  • Related: Why do schools teach arrays over List? – Robert Harvey Jun 8 '16 at 2:15
  • In C# arrays are messed up as well im many ways. In C/C++ an array is not a value which breaks the style of the language. JavaScript arrays also have their issues. They are special and strange in many ways. – usr Jun 8 '16 at 13:16
5

In Java, there is no Array type collection to make arrays feel more consistent, such as in inheritance.

Sure there is, it's called List<T>. The only fundamental difference between a hypothetical Array<T> and List<T> is that you can't resize the Array<T>. If Array<T> were added it would be just like List<T> except with less functionality, and what would be the point of that?

Sometimes it makes sense to have a fixed number of elements, some of which may not be set at a point in time. You might imagine a row of cupholders; you can put cups in any position or take them out, but you can't just add another cupholder.

And you can do that with a List<T>.

List<String> strings = Arrays.asList(new String[45]);

If you don't want to resize your lost, don't resize your list. Any additional functionality available on your data structure you don't need is not hurting you.

However, having extra functionality can be bad when it makes code harder to reason about. Consider:

FooResult result = calculateFoo(myList);

If calculateFoo takes an interface that cannot resize myList then it will be easier to understand what the code does. I know the list will be the same size afterwards. But it would be even better if myList was immutable, and then I would know it hasn't change at all.

Having a fixed-size list doesn't seem very useful because either I want a list I can modify (in which case have the option to resize doesn't hurt), or I want a list that cannot be modified. An array type suits neither purpose.

0
4

Regarding Java, Java 1.0 didn't have generics. On the contrary, I'd argue arrays are "first-class citizens" in Java since they're given special treatment by the language.

It's true that sometimes you simply need a fixed-size collection, but the case where you need a dynamically-sized collection is more common.

Finally, arrays are fine for temporary usage in some algorithm, but leave much to be desired as function arguments or return values. The fact that they're mutable means you have to do defensive copying when passing them to functions since you don't know if the function will want to mutate the contents. The same is true when returning arrays from a getter - you usually don't want the caller to be able to change your object's private variables, so a defensive copy is required.

Eric Lippert has a great article on this: Arrays considered somewhat harmful

3
  • In java arrays can contain value types, which generic collection can not. So I'd rather call generics second class citizens. – CodesInChaos Jun 8 '16 at 7:24
  • @CodesInChaos I suppose it's a matter of perspective, but in my brain the first-class citizen is the one that's treated better. Generics are the second-class citizen in your scenario. It's no secret they implemented poorly to preserve backwards compatibility. – Doval Jun 8 '16 at 12:36
  • I guess what I was saying is that it's interesting how Java has Collections.unmodifiableList but since arrays are "primitive" objects, there can't be a Collections.unmodifiableArray and stuff like that. – Justin Jun 22 '16 at 22:13
2

Arrays are very low level. You could as well ask, "In java why are int, float, and double not objects, to make them more consistent with the rest of the object-orientated language?"

Arrays have a close mapping (in most languages) to the assembly level memory code. This has no real notion of generics, arrays are of pointers or other primitive types. Arrays exist in higher level languages, mostly as tools to build more sophisticated data-structures upon.

A high-level fixed sized construct likely exists in your language that is a "first class citizen". Possibly tuple or FixedSizeVector

4
  • "You could as well ask, 'In java why are int, float, and double not objects, to make them more consistent with the rest of the object-orientated language?'" - I don't really understand your point here since int, float and double are objects in Java. – Alternatex Jun 8 '16 at 12:14
  • Not unless the Java spec has changes recently. Last I checked int, float , double etc are primitives. See stackoverflow.com/questions/8660691/… – Lyndon White Jun 8 '16 at 12:35
  • Ahhh I see, my mistake. I find it very surprising that they aren't synonymous with Integer, Float and Double but completely different data types. – Alternatex Jun 8 '16 at 12:43
  • They are linked to them, but unlike in C# they are not synonymous. – Lyndon White Jun 8 '16 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.