0

I am not asking about the difference between Arrays and ArrayList<> as I "guess" I know most of them, a brief summary of the most important ones:

  • Fixed length data for Arrays, while dynamic and automatically growing size for ArrayList<>
  • Use of generics (ArrayList<>)
  • String primitives (Arrays of Objects, but it's off the topic of my subject here, as I am talking about Arrays of Objects)
  • Several small differences on variable and method naming: length, size(), add()

So, IMO the most important points indicate that we should use ArrayList<> over arrays of object, even answers of Array or List in Java. Which is faster? indicate that ArrayList<> are faster or more advised! I can easily recognize when I should use ArrayList<> but I can't see any specific use case for Arrays of Objects!

In a nutshell, when should we use Arrays of Objects?

Community
  • 1
  • 1
Tarik
  • 4,961
  • 3
  • 36
  • 67
  • 1
    i don't find a good reason but, Object[] is covariant for any type.. – nachokk Feb 11 '15 at 02:25
  • thanks for replying, but i really hope if someone dwonvote to explain why? i have no problem with downvoting but an explanation would be very helpful to me, thanks – Tarik Feb 11 '15 at 02:33
  • If you didn't know.. doing array.length; doesn't have () parenthesis because it's referencing the public length field variable from the array class. – Woodrow Feb 11 '15 at 02:38
  • 1
    @Woodrow in my question, i didn't put any `()` for the variable `length` ! – Tarik Feb 11 '15 at 02:46
  • My bad Tarik - I misread. – Woodrow Feb 11 '15 at 02:52

4 Answers4

1

arrays have at least one thing that generics don't. they have their type at runtime. therefore they are assignable one from another. if you need that kind of information (e.g. you create some kind of framework that assigns parameters of different types by reflection or tries to find best match for a method signature etc) than arrays are a bit easier. but in regular development lists are usually a better choice

piotrek
  • 13,982
  • 13
  • 79
  • 165
  • do you mean runtime or compile time for arrays?! – Tarik Feb 11 '15 at 02:31
  • compile time for arrays and lists. runtime only for arrays – piotrek Feb 11 '15 at 02:33
  • true. but Integer[] does have more information. and you can assign it to variable of type Object[]. it's not that easy with generics at runtime when you instect the code with reflection – piotrek Feb 11 '15 at 02:47
0

With Java's modern collection library there is no longer any reason to use native or primitive arrays in your own code: the collections are more flexible and safer. The only time you should need to deal with primitive arrays is when you are forced to through integration with an interface you can't change. An example of this is the arguments passed to your main method.

In my view the #1 advantage of using collections is that you can later change your mind about the implementation without impacting other code. Switching from List<Integer> to Set<Integer> is a lot easier than from int[] to Set<Integer>.

sprinter
  • 27,148
  • 6
  • 47
  • 78
  • @but in that case why the javadocs doesn't recommand or deprecate them ?! – Tarik Feb 11 '15 at 02:31
  • @Tarik backward compatibility – sprinter Feb 11 '15 at 02:32
  • that why we can use the deprecated, no ? – Tarik Feb 11 '15 at 02:34
  • @Tarik I suspect getting enough of the Java community to agree with my answer above would be more trouble than it's worth! – sprinter Feb 11 '15 at 02:36
  • There are a significant performance and efficiency reasons to prefer arrays over Collections for primitives. – Brett Okken Feb 11 '15 at 02:40
  • 1
    @BrettOkken ArrayList is just a pretty thin wrapper around an array. I don't think there are any significant performance differences between them (not in my testing in any case). – sprinter Feb 11 '15 at 02:43
  • @BrettOkken see http://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster – sprinter Feb 11 '15 at 02:44
  • 1
    I am speaking specifically about primitives, that post is about Strings. Consider an `int[]` to an `ArrayList` each with 100 entries. The `int[]` will require about 412 contiguous bytes of memory. Lets assume that the `List` was sized exactly correct. In this case it is backed by an `Object[100]` which will consume the same 412 contiguous bytes (assuming compressed oops in 64 bit). It will contain references to 100 Integer objects located throughout memory and consuming another ~16 bytes apiece, for a total of ~2200 bytes. So it consumes 3x the amount of memory. – Brett Okken Feb 11 '15 at 02:52
0

If you know you will always have a fixed length/size, then working with a fixed length array makes sense. This allows you to encapsulate your intentions. It is more of a design decision than a performance consideration. Flexibility is not ALWAYS the best solution.

The Gilbert Arenas Dagger
  • 12,071
  • 13
  • 66
  • 80
  • but isn't a restriction? having a dynamic size doesn't have any real inconvenient i think – Tarik Feb 11 '15 at 02:30
  • 1
    That's exactly the point. It is a restriction. Sometimes you want that. If I have a Cube object, then Side[] sides makes more sense to me then List sides. There is no need to ever add another side to the cube... there will always be exactly 6. Allowing someone to add another side allows someone to use my cube object in a way different than how it was intended – The Gilbert Arenas Dagger Feb 11 '15 at 02:34
0

Because Arrays is simple. It depends on your requirement. When you

  • have a fixed length of data, be it an Object or a primitive type

  • only need to perform basic set(store) and get(retrieve) operation

the first thing that comes to your mind is Arrays instead of ArrayList. Simplicity is self explanatory.

Wilts C
  • 1,720
  • 1
  • 21
  • 28