Sobes.tech
Junior — Middle

How can you determine the duration of operations with an ArrayList?

sobes.tech AI

Answer from AI

To determine the duration of operations with ArrayList in Java, you can use the System class and methods for measuring time, such as System.nanoTime() or System.currentTimeMillis().

Example of measuring the time to add elements:

import java.util.ArrayList;

public class ArrayListTiming {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        long startTime = System.nanoTime();

        for (int i = 0; i < 100000; i++) {
            list.add(i);
        }

        long endTime = System.nanoTime();
        long duration = endTime - startTime; // time in nanoseconds

        System.out.println("Time to add 100000 elements: " + duration + " ns");
    }
}

This way, you can measure the execution time of various operations (adding, removing, searching, etc.) and evaluate their performance.

How can you determine the duration of operations with… - sobes.tech