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?

5
  • 1
    i don't find a good reason but, Object[] is covariant for any type.. Commented Feb 11, 2015 at 2: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 Commented Feb 11, 2015 at 2: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. Commented Feb 11, 2015 at 2:38
  • 1
    @Woodrow in my question, i didn't put any () for the variable length ! Commented Feb 11, 2015 at 2:46
  • My bad Tarik - I misread. Commented Feb 11, 2015 at 2:52

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

3 Comments

do you mean runtime or compile time for arrays?!
compile time for arrays and lists. runtime only for arrays
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
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>.

8 Comments

@but in that case why the javadocs doesn't recommand or deprecate them ?!
@Tarik backward compatibility
that why we can use the deprecated, no ?
@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).
I am speaking specifically about primitives, that post is about Strings. Consider an int[] to an ArrayList<Integer> each with 100 entries. The int[] will require about 412 contiguous bytes of memory. Lets assume that the List<Integer> 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.
|
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.

Comments

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.

2 Comments

but isn't a restriction? having a dynamic size doesn't have any real inconvenient i think
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<Side> 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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.