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 15 of 100

What is the difference between an array and a List in Java?

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

In Java, an array is a fixed-size data structure that stores a sequence of elements of the same type, while a List is an interface in the Java Collections Framework that represents a variable-size sequence of elements of any type. The main differences between arrays and Lists are as follows:

Size: Arrays have a fixed size, meaning that once an array is created, its length cannot be changed. Lists, on the other hand, have a variable size, meaning that elements can be added or removed from the List at any time.

Type: Arrays can only store elements of a single type, while a List can store elements of any type.

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

Performance: Arrays generally have better performance than Lists for simple operations such as element access and iteration. However, Lists 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 List in Java:

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

public class Example {
    public static void main(String[] args) {
        // create an array of integers
        int[] myArray = {1, 2, 3};
        
        // create a List of integers
        List<Integer> myList = new ArrayList<>(Arrays.asList(1, 2, 3));
        
        // add an element to the array and the List
        myArray[3] = 4;
        myList.add(4);
        
        // 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 a List of integers called "myList". We then add an element to both the array and the List and use the toString() method to print out their contents.

The output of this program would be:

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

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

In summary, arrays and Lists are both used to store sequences of elements in Java, but they have some key differences in terms of size, type, direct 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