WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Basic · question 14 of 100

What is the difference between Arrays and Collections in Java?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

Arrays and Collections are both used in Java to store and manipulate groups of related data, but they have some key differences:

Fixed vs. dynamic size: Arrays have a fixed size, meaning that once they are created, their size cannot be changed. Collections, on the other hand, have a dynamic size, meaning that elements can be added to or removed from the collection at any time.

Primitives vs. objects: Arrays can store both primitives (int, char, boolean, etc.) and objects, while most collections can only store objects. However, there are some specialized collections that can store primitives, such as the IntArrayList in the Apache Commons library.

Direct access vs. indirect access: Elements in an array can be directly accessed using an index, while elements in a collection can only be accessed indirectly through an iterator or a specific collection method.

Performance: Arrays generally have better performance than collections for simple operations such as element access and iteration. However, collections are generally more convenient and easier to work with for complex operations such as sorting and filtering.

Here’s an example of how to use an array and a collection in Java:

import java.util.ArrayList;
import java.util.Arrays;

public class Example {
    public static void main(String[] args) {
        // create an array of integers
        int[] myArray = {1, 2, 3};
        
        // create an ArrayList of integers
        ArrayList<Integer> myList = new ArrayList<>(Arrays.asList(1, 2, 3));
        
        // print out the contents of the array and the list
        System.out.println("Array: " + Arrays.toString(myArray));
        System.out.println("List: " + myList);
    }
}

In this example, we create an array of integers called "myArray" and an ArrayList of integers called "myList". We then use the toString() method to print out the contents of both the array and the list.

The output of this program would be:

Array: [1, 2, 3]
List: [1, 2, 3]

As you can see, both the array and the list contain the same elements.

In summary, Arrays and Collections are both used to store and manipulate groups of related data in Java, but they have some key differences in terms of fixed vs. dynamic size, primitives vs. objects, direct vs. indirect access, and performance. Which one to use depends on the specific use case and requirements of your program.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics