In which situation Arrays and ArrarList should be used?


Arrays are the most commonly used data structure to store a collection of elements. Most programming languages provide methods to easily declare arrays and access elements in the arrays. An arraylist can be seen as a dynamic array, which can grow in size. Due to this reason, the programmer does not need to know the size of the arraylist when she is defining it.

What are Arrays?

Shown in figure 1, is a piece of code typically used to declare and assign values to an array. Figure 2 depicts how an array would look like in the memory.
int values[5];

values[0]=100;

values[1]=101;

values[2]=102;

values[3]=103;

values[4]=104;



Figure 1: Code for declaring and assigning values to an array

100 101 102 103 104
Index: A 0                             1                              2                              3                              4

Figure 2: Array stored in the memory

Above code, defines an array that can store 5 integers and they are accessed using indices 0 to 4. One important property of an array is that, entire array is allocated as a single block of memory and each element gets its own space in the array. Once an array is defined, its size is fixed. So if you are not sure about the size of the array at compile time, you would have to define a large enough array to be in the safe side. But, most of the times, we are actually going to use less number of elements than we have allocated. So a considerable amount of memory is actually wasted. On the other hand if the 'large enough array' is not actually large enough, the program would crash.

What are Arraylists?

An arraylist can be seen as a dynamic array, which can grow in size. Therefore arraylists are ideal to be used in situation in which you don't know the size of the elements required at the time of declaration. In Java, arraylists can only hold objects, they cannot hold primitive types directly (you can put the primitive types inside an object or use the wrapper classes of the primitive types). Generally arraylists are provided with methods to perform insertion, deletion and searching. Time complexity of accessing an element is o(1), while insertion and deletion has a time complexity of o(n). In Java, arraylists can be traversed using foreach loops, iterators or simply using the indexes.

What is the difference between Arrays and Arraylists

Even though the arrays and arraylists are similar in the sense that both of them are used to store collections of elements, they differ in how they are defined. The size of the array has to be given when an array is defined, but you can define an arraylist without knowing the actual size. You can add elements to an arraylist after it is defined and this is not possible with arrays. But in Java, arraylists cannot hold primitive types, but arrays can be used to hold primitive types. But if you need a data structure that can vary its size, arraylist would be the best choice.

People who read this post also read :



0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More