1

What determines whether one should be used over the other?

I used to think that the deciding factor is whether you know the size of the things you want to store but I think there might be more to it than that.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
Ogen
  • 6,499
  • 7
  • 58
  • 124
  • possible duplicate of [Array or List in Java. Which is faster?](http://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster) – Vignesh Vino Jul 21 '14 at 08:45
  • I'm not looking for which is faster. I'm looking for when to use one over the other, speed is not the only factor im sure – Ogen Jul 21 '14 at 08:47

5 Answers5

4

Some more differences:

  • First and Major difference between Array and ArrayList in Java is that Array is a fixed length data structure while ArrayList is a variable length Collection class. You can not change length of Array once created in Java but ArrayList re-size itself when gets full depending upon capacity and load factor. Since ArrayList is internally backed by Array in Java, any resize operation in ArrayList will slow down performance as it involves creating new Array and copying content from old array to new array.

  • Another difference between Array and ArrayList in Java is that you can not use Generics along with Array, as Array instance knows about what kind of type it can hold and throws ArrayStoreException, if you try to store type which is not convertible into type of Array. ArrayList allows you to use Generics to ensure type-safety.

  • One more major difference between ArrayList and Array is that, you can not store primitives in ArrayList, it can only contain Objects. While Array can contain both primitives and Objects in Java. Though Autoboxing of Java 5 may give you an impression of storing primitives in ArrayList, it actually automatically converts primitives to Object.

  • Java provides add() method to insert element into ArrayList and you can simply use assignment operator to store element into Array e.g. In order to store Object to specified position.

  • One more difference on Array vs ArrayList is that you can create instance of ArrayList without specifying size, Java will create Array List with default size but its mandatory to provide size of Array while creating either directly or indirectly by initializing Array while creating it. By the way you can also initialize ArrayList while creating it.

Braj
  • 46,415
  • 5
  • 60
  • 76
3
  • Use array when you know the exact size of the collection and you don't expect to add/remove elements.

  • Use List (ArrayList) when you don't know the exact size of the collection and you expect to alter it at some point.

  • If you're using Java8, there is the Stream API, which helps to significantly reduce the boilerplate code when working with collections. This is another plus for ArrayList (and all Collections and Maps).

More info:

Community
  • 1
  • 1
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • What about if I know the exact size initially but I expect it to change? – Ogen Jul 21 '14 at 08:52
  • 1
    Then use ArrayList. Arrays are with specific size and if you expect it to change, you will need to pre-initialize the array – Konstantin Yovkov Jul 21 '14 at 08:53
  • Thanks for the help. I just have one more question/scenario. Say I know the size and I know it won't change, are there still reasons (other than performance) to use an ArrayList rather than an array? – Ogen Jul 21 '14 at 08:55
  • ArrayList provides a great variety of methods which can ease your work with the collection. Especially in Java8, there is the so called Stream API, which can help **significantly** to reduce the boilerplate code when using collections. – Konstantin Yovkov Jul 21 '14 at 08:58
2

Unless speed is critical (really critical, like every microsecond counts), use ArrayList whenever possible. It's so much easier to use, and that's usually the most important thing to consider.

user253751
  • 57,427
  • 7
  • 48
  • 90
0

Generally, I use ArrayList, not arrays, because they offer a lot of several methods that are very usefull. I think you can use array if performance is very important, in very special cases.

funkygono
  • 426
  • 3
  • 5
  • Your advice is good but the reasoning could be flawed. There are several useful methods available to `Arrays` as well. – ChiefTwoPencils Jul 21 '14 at 08:44
  • Yes, that's true, but working with arrays make it complicated if you have to resize your list. Furthermore, it is very difficult to protect array access if you have to expose your array. With list, you can easilly wrap it in an immutable list (see Collections class or the Guava equivalent). – funkygono Jul 21 '14 at 08:48
0

Array is fixed, ArrayList is growable.If the number of elements is fixed, use an array Also one of the great benefits of collection implementations is they give you a lot of flexibility. So depending on your need, you can have a List behave as an ArrayList or as a LinkedList and so on. Also if you look at the Collection API, you'd see you have methods for almost everything you'd ever need to do.