1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Arada [10]
4 years ago
11

The ArrayList class contains a trim method that resizes the internal array to exactly the capacity. The trim method is intended

to be used after all the items have been added the ArrayList, in order to avoid wasting space. Suppose, however, the novice programmer invokes trim after each add. In that case, what is the running time of building an N-item ArrayList? Write a program that performs 100,000 adds to an ArrayList and illustrates the novice’s error.
Computers and Technology
1 answer:
andreev551 [17]4 years ago
5 0

Answer:

public void trimToSize() {

modCount++;

if (size < elementData.length) {

elementData = (size == 0)

? EMPTY_ELEMENTDATA

: Arrays.copyOf(elementData, size);

}

}

Now, the running time for copyOf() function is O(N) where N is the size/capacity of the ArrayList. Hence, the time complexity of trimToSize() function is O(N).

Hence, the running time of building an N-item ArrayList is O(N^2).

Please find the sample code below.

CODE

==================

import java.util.ArrayList;

public class Driver {

  public static void main(String[] args) throws Exception{

      int N = 100000;

      ArrayList<Integer> arr = new ArrayList<>(N);

     

      long startTime = System.currentTimeMillis();

      for(int i=0; i<N; i++) {

          arr.add(i);

          arr.trimToSize();

      }

      long endTime = System.currentTimeMillis();

      double time = (endTime - startTime)/1000;

      System.out.println("Total time taken = " + time + " seconds.");

  }

}

Explanation:

You might be interested in
5) Match the following.
statuscvo [17]
1 .hub
2.server
3.Network Interface
4.Workstation


I’m sorry if I’m wrong :(

I hope it helps
8 0
3 years ago
Why would a brokered CD pay more than a regular CD?
Lana71 [14]

Brokered CDs may have higher or lower rates than those purchased directly from bans and credit unions. The general consensus here is that higher rates are usually available for direct purchases.

3 0
3 years ago
An engineer plans to connect three switches (SW1, SW2, and SW3) in a lab. Before connecting the switches, he starts by configuri
g100num [7]

Answer:

c. All use SW3's database because SW3 has the highest revision number.

Explanation:

In this particular configuration, the three switches will definitely be connected to one another by the presence of some path with the specific trunks. As a result, the database with the most revision number will ultimately win. The process of the STP election does not have any influence on the selection of the VTP.

4 0
3 years ago
What is required to become a good critical thinker? Use a process when dealing with a big issue. Practice the right skills over
Damm [24]

Answer:

Evaluate issues based on logic and reason

Explanation:

Critical Thinking is the ability to think scientifically & logically, analysing based on both formers - rather than rule of thumb.

Critical thinker engages clearly in conceptual, rational thinking. So, an important desirable characteristic of a critical thinker is 'evaluating issues based on logic and reason'.

6 0
3 years ago
The mode is least appropriate for:___________
bazaltina [42]

Answer:

Your question seems incomplete

Explanation:

7 0
3 years ago
Other questions:
  • If you use the ___ template, as opposed to a formatted theme, you must make all design decisions?
    15·1 answer
  • What is the most widely used operating system for mobile devices?
    7·1 answer
  • Edmund wants to create a website for his university. He has created all the necessary content and now wants to style and format
    11·1 answer
  • You must establish credit in order to buy a house true or false
    5·2 answers
  • Suppose your friend, who is a small business owner, wants to computerize her business functions such as accounting, payroll, and
    7·1 answer
  • 1. The first popular animated feature film was 1937’s Snow White and the Seven Dwarfs. (1 point)
    8·1 answer
  • Lucy wants to add some notes in her research paper that provide additional information to the reader. She also wants to display
    14·1 answer
  • Which three IP addresses may be pinged by the Test Management Network feature in the ESXi hosts DCUI
    12·1 answer
  • Denelle, an accountant, needs to present her client Stephen with a page showing how his yearly retirement savings will be affect
    11·1 answer
  • Hello. I really need this done ASAP. Don't scroll down. JUst look please.
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!