DEV Community

Cover image for Understanding ArrayList: Essential Knowledge for Interviews
Arshi Saxena
Arshi Saxena

Posted on • Edited on

Understanding ArrayList: Essential Knowledge for Interviews

In this post, we'll explore the basics of the ArrayList in Java, one of the most commonly used collections. We'll cover how to initialize an ArrayList, its properties, and answer some common interview questions related to it.

What is an ArrayList?

ArrayList is part of the Java Collections Framework and implements the List interface. It is an ordered collection that allows duplicates. Here are some key features:

  • Dynamic resizing: Unlike arrays, ArrayLists can grow and shrink in size as needed.
  • Allows duplicates: You can add the same element multiple times.

Ways to Initialize an ArrayList

1. Using the Default Constructor: This creates an ArrayList with a default initial capacity of 10.

   ArrayList<Integer> defaultList = new ArrayList<>();
Enter fullscreen mode Exit fullscreen mode

2. Using a Parameterized Constructor with Initial Capacity: You can specify the initial capacity of the ArrayList to optimize performance. This is particularly useful when you have an idea of how many elements will be added to the list.

   ArrayList<Integer> initialCapacityList = new ArrayList<>(5);
Enter fullscreen mode Exit fullscreen mode

Here are some benefits of using a parameterized constructor with initial capacity:

  • Performance: Setting an initial capacity reduces the overhead associated with resizing the ArrayList as elements are added. This minimizes the need for the list to reallocate and copy its contents, leading to better performance.

  • Memory Management: Allocating memory efficiently helps avoid frequent resizing, which can be resource-intensive. By initializing the ArrayList with the expected number of elements, you can improve memory usage and overall application performance.

3. Using a Parameterized Constructor with a Collection: You can initialize an ArrayList with a predefined collection.

// Initializing at declaration
ArrayList<Integer> collectionList = new ArrayList<>(Arrays.asList(1, 2));
// Adding elements one by one
collectionList.add(1);
Enter fullscreen mode Exit fullscreen mode

Important Note

It's essential to understand that the size of an ArrayList is not the same as its initial capacity. The size refers to the actual number of objects stored in the list.
For example:

ArrayList<Integer> initialCapacityList = new ArrayList<>(5);
System.out.println(initialCapacityList.size()); // Result: 0
Enter fullscreen mode Exit fullscreen mode

Common Interview Questions

1. How to get the index of an element in an ArrayList?

You can use the indexOf() method, which returns the first occurrence of the specified element in the list.

ArrayList<Integer> collectionList = new ArrayList<>(Arrays.asList(1, 2, 1));
System.out.println(collectionList.indexOf(1));
// Result: 0 (only first occurrence)
Enter fullscreen mode Exit fullscreen mode

2. How to find the first and last occurrence of the same element?

You can use both indexOf() and lastIndexOf() methods.

ArrayList<Integer> collectionList = new ArrayList<>(Arrays.asList(1, 2, 1));
System.out.println(collectionList.indexOf(1)); // Result: 0
System.out.println(collectionList.lastIndexOf(1)); // Result: 2
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this post, we covered the basics of ArrayList, how to initialize it, and some common interview questions. Understanding these fundamentals will help you build a strong foundation in Java collections.

Stay tuned for the next post in the Java Collections Essentials series, where we'll dive deeper into other collection types and their features!

Related Posts

Happy Coding!

Top comments (2)

Collapse
 
shshlomy profile image
shlomy sheps

Great artical,
one comment :it better to program into an interface:
List defaultList = new ArrayList<>();

this way it can be changed in the future.

Collapse
 
arshisaxena26 profile image
Arshi Saxena

Thank you for your comment and for highlighting a key best practice in Java programming—programming to an interface! You're absolutely right; declaring the variable as List rather than ArrayList can offer more flexibility, allowing the implementation to be switched easily if needed.

For the purposes of this article, I chose ArrayList explicitly to emphasize the properties and methods specific to ArrayList itself, which are commonly tested in interviews. This helps beginners focus on understanding ArrayList's unique behaviors, like dynamic resizing and random access, rather than the broader interface.

I appreciate your feedback, as it's a great reminder of the importance of programming to interfaces, especially in scalable and adaptable codebases. Thanks again for sharing this valuable insight!