In this article we will cover frequently asked ArrayList interview questions.
- How can we obtain an array from an arraylist class ?
- How to convert an array to arraylist?
- ArrayList is resizable . So, can we add elements to an ArrayList obtained from Arrays.asList() ?
- What is the differences between ArrayList and Vector ?
- How can we synchronize an ArrayList ?
- Does ArrayList allow duplicates to be added ?
- Does ArrayList allow Null values ?
- What is the difference between ArrayList clear() and removeAll() methods ?
- What is the difference between ArrayList’s overloaded remove() methods ?
- If an ArrayList contains duplicate objects and remove() method is invoked on the same object, will it remove the duplicates?
How can we obtain an array from an arraylist ?
We can obtain the array from an arraylist using toArray() method of ArrayList.
List<Integer> numbers = new ArrayList<Integer>();
Integer arr = new Integer[numbers.size()];
arr = numbers.toArray(arr);
Refer this article for details:
How to convert an array to arraylist?
Arrays.asList() method can be used to create a fixed-size arraylist from an array.
List list = Arrays.asList(arr);
Refer this article for details:
Can we add elements to an ArrayList obtained from Arrays.asList() ?
Arrays.asList() method creates a fixed-size arraylist from an array. We can not add more elements to it.
Refer following puzzle for this :
What is the differences between ArrayList and Vector ?
– ArrayList is not synchronized whereas Vector is synchronized.
– ArrayList allows a null key and null values. Vector does not allow null keys or values.
– Since methods in ArrayList are not synchronized, ArrayList performs better compared to a Vector. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.
How can we synchronize an ArrayList ?
ArrayList can be synchronized using Collections.synchronizedList() method as follows:
List list = Collections.synchronizedList(new ArrayList(...));
Does ArrayList allow duplicates to be added ?
Yes, ArrayList allows duplicates.
Does ArrayList allow adding NULL values?
Yes, ArrayList permits adding NULL elements.
What is the difference between ArrayList’s clear() and removeAll() methods ?
clear() method removes all the elements from an ArrayList.
removeAll() method takes a collection as parameter. It removes all of the ArrayList elements that are part of the collection.
What is the difference between ArrayList’s overloaded remove() methods ?
ArrayList has two overloaded remove methods: remove(int) and remove(Object)
remove(Object) removes the Object from the arraylist.
remove(int) removes the element at the index specified in the parameter.
Here is an interesting puzzle about these overloaded methods that you may like :
Java Quiz 32 (Overloaded remove() methods)
If an arraylist contains duplicate objects and remove() method is invoked on the same object, will it remove the duplicates?
ArrayList’s remove(Object) operation removes the first occurrence of the match found. It does not remove the duplicates.
Here are some more articles you might like :
- Understanding ArrayList in java
- HashMap Interview Questions in java
- Understanding ArrayList remove() methods in java
- Top 10 Articles on Java Collections
- Java Quiz 31(ArrayList)
© 2016, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post