-5

I believe Array and ArrayList both are non-Synchronised;behave same in multiThreaded env. And both are Index based. Only Adavantage is ArrayList has utility methods and flexible.
Apart from those Utility methods only to get and set fixed no of Objects which is best in java? Is there any overhead in using Arraylist than Array? Pls explain.

Let us consider Scenario like

1) 1,00,000 Objects to be stored. 2) 1,00,000 primitives to be stored

Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101
  • 1
    You forgot about HashSet, TreeSet, HashMap (with Integer index), TreeMap, LinkedList and millions of other "Collection" implementations. – bezmax Dec 19 '11 at 13:21
  • "What is the best X?" is almost never a good question. Nothing in technology is "the best" at everything and for every use case. – millimoose Dec 19 '11 at 13:24

4 Answers4

4

To be able to answer question on which data representation fits better, one needs to know many details like:

  • how many elements will be stored in the structure?
  • will the structure be mutable or immutable, that is, will the contents of the it change after initialization?
  • do you know the final size of your structure when you initialize it?
  • how will you access the elements? iterate over it? random access by index?
  • etc.

Regarding the array vs. arraylist: arraylist performance overhead is minimal and in most cases it is not significant. On the other hand, it is more flexible. see this thread Array or List in Java. Which is faster?

Community
  • 1
  • 1
bpgergo
  • 15,669
  • 5
  • 44
  • 68
  • •how many elements will be stored in the structure? Please consider both 10 Objects and 1,00,000 objects •will the structure be mutable or immutable, that is, will the contents of the it change after initialization? Yes. It will be changed with some other objects – Kanagavelu Sugumar Dec 19 '11 at 13:40
2

A ArrayList uses internally an Array. So yes, an ArrayList has a little bit of overhead, but it provides you with some convienent methods to access the elements in the list. It depends what you prefer.

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
2

ArrayLists will have an ever so slightly higher overhead than an array. This is because an ArrayList is just a wrapper for an array with a few convenience methods added. Though if you know exactly how many elements you will ever have then ArrayList doesn't add much.

The biggest advantage of having an array over an ArrayList (at least in Java), is the cleaner syntax. eg.

myArray[0] = x;
// as opposed to
myList.set(0, x); 
// personally I can never remember the order for the arguments and I have to check

Adding all the elements to another list

One possible advantage to ArrayLists is being able to easily add all the elements in an ArrayList to another list.

anotherList.addAll(myList);

However, the same can easily be accomplished by using the Collections util class.

Collections.addAll(anotherList, myArray);

Immutability

One problem is that arrays are mutable (element values can be changed), but you may only want to present a immutable view of your collection of elements to classes outside your own class. Here lists can be useful again.

List<E> readOnly = Collections.unmodifiableList(myList);

But in this case also Arrays can still easily be used to to back an immutable list.

List<E> readOnly = Collections.unmodifiableList(Arrays.toList(myArray));

Synchronized access

Finally, and most importantly, you can make use of the Collections class to make your Lists synchronized.

List<E> syncedList = Collections.synchronizedList(myList);

You could wrap your array in a list and make access synchronized, but then you'd still have to use access via the list, so there wouldn't be much point in having an array.

Final word - Personal preferences

In general though, the whole thing comes down to preference. What is your personal preference, do you prefer working with arrays or Lists? Personally I prefer lists because they work better with the rest of the collections API and making a generic Lists is easy, making generic arrays is a real pain.

List<E> list = new ArrayList<E>(); // easy
E[] array = new E[]; // compilation error
Dunes
  • 37,291
  • 7
  • 81
  • 97
  • Thanks Dunes. I fully agree with your points 1) generic arrays is a real pain 2) Synchronized access. So let me use List in my code even it is fixed elements. – Kanagavelu Sugumar Dec 20 '11 at 05:25
0

If count of Objects is fixed, obviously Array is better solution. Because ArrayList uses array which can be created several times during adding.As javadoc says in array list function

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.

narek.gevorgyan
  • 4,165
  • 5
  • 32
  • 52
  • 1
    If you know how many objects are going to be stored in advance then you can create the `ArrayList` with the appropriate capacity, avoiding reallocation. – Jesper Dec 19 '11 at 14:30