Array and ArrayList in Java | Selenium Interview Question

Recently one of my friend has gone for interview in one of the leading travel domain company where he was asked about Array and ArrayList and interviewer was keen about performance of value insertion in Array and ArrayList.

So lets begin with small introduction of Array and ArrayList.

Array is a data structure defined in core java which stores multiple data of same type (Can store Object as well) and it is of fixed size.But if you want to learn more about Array and its implementation then I would suggest you, Read Arrays in Java and its implementation in WebDriver.

Similarly ArrayList is part of Collection Framework which store similar kind of Objects and in the core of it, it use array. So if you want to learn more about ArrayList then I would suggest you to read Java Doc For ArrayList .

So up to now we understand that both stores the data of same type but one have primitive data type along with object and other only takes object. But this is not the only thing that we need to remember. There are some parameters over whichArray and ArrayList we need to analyse.

Primitive: Array can store primitive data type like int, char, double etc and objects While ArrayList can only store objects in it.

But if you want to add any primitive in ArrayList in that case it converts primitive data type to equivalent Object define in Java using Auto-boxing.

ArrayList al = new ArrayList();
al.add(1);

So here 1 which is int, internally get converted to Integer Object.

Size: 1- In Layman term storage capability of Array is fixed while ArrayList has dynamic storage capability.
2- In Array we define size of array at time of its initialisation while In ArrayList we never define the size at the time of its initialisation. But internally ArrayList use concepts of Array and when ever we create any ArrayList, it creates one Array of some specific size and once this size is attained, It dynamically creates array of double of initial size and copy initial data in new array that is dynamically created.

 

Support to Generics: 1- ArrayList is type-safe while Array is not.

2- Type-safety in Java is handled using Generics and ArrayList use generics while this can’t be used in array. If any time we try to use generics in that case it throws exception ArrayStoreException because instance of Array would not be able to convert it in to the type defined for Array.

Dimensions: Array can be defined multi-dimensional while ArrayList can be one dimensional.

Iteration: If user wants to iterate through each element of ArrayList then he/she needs to use Iterator from Java or For each loop to look into or to access each element, While it can be done through for loop and For Each loop for array.

Performance: this is one factor that takes constant time in both (Up to initial size of ArrayList) if we are calling add() and get() in ArrayList.But automatic resizing in ArrayList might cause slowness in insertion of data (Adding Data in ArrayList).

Length: Array uses length variable to find actual size or length of array but in case of ArrayList, we need to call size() function to find the length of ArrayList.

So this was the basic difference between Array and its dynamic brother ArrayList.

Hope this post will be helpful.

Happy Learning…

If you want to learn more about Java then please read Java Tutorial.

 

 

Dwarika Dhish Mishra

My name is Dwarika Dhish Mishra, its just my name and I am trying to bring the worth of my name in to actions and wants to be the solution not the problem. I believe in spreading knowledge and happiness. More over I am fun loving person and like travelling a lot. By nature I am a tester and a solution maker. I believe in the tag line of http://ted.org “Idea worth spreading” . For the same, I have created this blog to bring more and more learning to tester fraternity through day to day learning in professional and personal life. All contents are the part of my learning and so are available for all..So please spread the contents as much as you can at your end so that it could reach to every needful people in testing fraternity. I am pretty happy that more and more people are showing interest to become the part your Abode QA blog and I think this is good sign for us all because more and more content would be before you to read and to cherish. You may write or call me at my Email id: dwarika1987@gmail.com Cell: 9999978609

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.