I am curious as to why Arrays are used when you could use an ArrayLists instead? Wouldn't it always be better to use an ArrayList?
-
2If the only language you ever plan on using is java then ya dont' worry about it. – Jake Sellers Aug 06 '13 at 16:13
-
2This is a relevant thread that might help you: http://stackoverflow.com/questions/717218/why-not-always-use-arraylists-in-java-instead-of-plain-ol-arrays – Brian Aug 06 '13 at 16:14
-
1Do you mean `Arrays` as in `java.util.Arrays` or "arrays" as a general computer science term for a contiguous block of memory accessed by index? – Ted Hopp Aug 06 '13 at 16:14
-
3Try defining an array list of primitives, and compare its performance to an array list of boxed primitives to see a good reason why you desperately need to learn about arrays :-) – Sergey Kalinichenko Aug 06 '13 at 16:16
-
1The overhead of an arraylist may be undesirable. Especially on systems where memory is scarce. A million elements in ArrayList
may be magnitudes bigger than a million elements in int[]. – arynaq Aug 06 '13 at 16:16 -
2Already some good answers here.Just want to clarify that ArrayList is actually a wrapper on top of your arrays or to be more specific Dynamic Arrays==ArrayList. So it is always better to know the main concept before the internal concept right? :) – Algorithmist Aug 06 '13 at 16:18
-
2Sometimes it is good to reinvent the wheel. You must know how the wheel works in order to tweak it. You don't always want to treat everything as a 'black box' in programming. Programming fundamentals, such as data structures, don't change as much over time and are so important to understand. – Paul Renton Aug 06 '13 at 16:20
-
1The simplest answer is "basics". Yes they allways are boring and don't seem to serve a purpose but later on you will notice that without those basics you won't be able to use whatever you learned properly. – Akunosh Aug 06 '13 at 16:21
-
If there were `ArrayList`s of primitives in the JDK, there sure would be very much less reason to ever use arrays. Sadly, this is not the case. – Marko Topolnik Aug 06 '13 at 16:40
6 Answers
Check out this comparison.
As you can see, there are important differences between the two constructs. You'll find APIs using one or the other (or both), and you have to understand the pros/cons and the functional differences between the two.
One particular difference is that a native array can store primitives without the inefficiencies of boxing/unboxing. That's significant when you have sizeable arrays representing data streams / data sets.
Note also that an ArrayList
is not covariant. That is, an Integer[]
is a Number[]
, but an ArrayList<Integer>
is not a ArrayList<Number>
. See here for more details.

- 268,207
- 37
- 334
- 440
Arrays and ArrayList
s serve different, though sometimes overlapping, purposes.
But aside from that, the simple fact is that you don't code in a vacuum, and so you're going to use APIs that involve arrays, so learning about them isn't optional. Arrays are an absolutely fundamental structure in computer science.

- 1,031,962
- 187
- 1,923
- 1,875
-
Actually I think the guy might code in a vacuum considering his question... – Zo72 Aug 06 '13 at 16:26
There are many things you can do with arrays that you can't do with ArrayList
s. For example,
- Read multiple bytes or code-units from a file using a
byte[]
or achar[]
- Call a native numeric or graphics library that deals with vectors, or matrices represented as
float[]
s ordouble[]
s
You can use other non-primitive-array abstractions instead of ArrayList
, but sometimes the best way to represent a contiguous region of memory divided into numeric units is using an array.

- 118,113
- 30
- 216
- 245
It is very important to have a proper foundation in programming fundamentals. These fundamentals include things like variable types, functions, arguments, arrays, and so on. Without a proper foundation, everything else you learn can collapse since you don't have a proper foundation. In other words, lets say you want to learn a language, you could start to learn whole phrases by memorization, but it is nice to get some words rolling off the tongue and start digesting and piecing how you can make sentences out of words instead of brute forcing it and memorizing all the sentences you need.

- 498
- 4
- 13
It is important for you to learn arrays if you wish to have a better understanding of what happens "behind the scenes", or if you want to learn a lower level language like C or assembly languages.
May I ask where you are learning Java? Is this for school/college? If so, please try to get something out of working with Arrays, because they will be used in courses like Data Structures and especially if you start using a language that doesn't have an ArrayList equivalent.
Learn all you can about it. Understanding what's happening is rewarding.

- 1,254
- 1
- 11
- 16
String []array = { "I","DO","NOT","RESIZE" };

- 14,593
- 17
- 71
- 103
-
@arshajii not at all. Just the simplest example I could think to show the differences between arrays and arraylists. – Zo72 Aug 06 '13 at 16:25