Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Significant differences in Array vs Array List? [duplicate]

Possible Duplicate:
When to use ArrayList over array[] in c#?

From the perspective of memory or processor costs, does there appear to be a significant difference between an array and an arrayList object?

like image 799
Mike Olson Avatar asked May 25 '12 01:05

Mike Olson


People also ask

What is the most important difference between array and ArrayList?

1) 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 the length of Array once created in Java but ArrayList re-size itself when gets full depending upon the capacity and load factor.

What are the differences between array and ArrayList?

As we all are aware of that arrays are linear data structures providing functionality to add elements in a continuous manner in memory address space whereas ArrayList is a class belonging to the Collection framework.

What is the difference and similarities between an array and a List?

Data Types Storage: Array can store elements of only one data type but List can store the elements of different data types too. Hence, Array stores homogeneous data values, and the list can store heterogeneous data values.

Which is more efficient array or ArrayList?

An array is faster and that is because ArrayList uses a fixed amount of array. However when you add an element to the ArrayList and it overflows. It creates a new Array and copies every element from the old one to the new one.


1 Answers

An array is a low-level data structure that essentially maps to a region in memory. An ArrayList is a variable length list implemented as an array of object that is re-allocated as the list grows.

ArrayList therefore has some overhead related to managing the size of the internal array, and more overhead related to casting objects to the correct type when you access the list.

Also, storing everything as object means that value types get boxed on write and unboxed on read, which is extremely detrimental to performance. Using List<T>, a similar but strongly-typed variable size list avoids this issue.

In fact, ArrayList is practically deprecated in favor of List<T> since .NET 2.0.

like image 101
Asik Avatar answered Nov 15 '22 23:11

Asik