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]
3 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]3 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
In addition to developing sketches, computer-aided design programs are used by fashion designers to perform which task?
nikdorinn [45]

Answer:

flat drawing, pattern making development, computerized marker planning and many other fashion designing related aspects.

Explanation:

6 0
2 years ago
Best programming languages? So far I have only learned html.​
Volgvan

Answer:

it's all up to personal preference some different ones have upsides and downsides but it's more of what you feel is best

4 0
3 years ago
Which type of service offers a preconfigured testing environment for application developers to create new software applications
gtnhenbr [62]

The type of service offers a preconfigured testing environment for application developers to create new software applications is Software as a Service (SaaS).

<h3>What is software as a service SaaS?</h3>

Software as a service (SaaS) is known to be a type of service that gives its users the ability to be able to link to and also use cloud-based apps in course of the Internet.

Conclusively, The type of service offers a preconfigured testing environment for application developers to create new software applications is Software as a Service (SaaS) as it gives its best service.

Learn more about application developers  from

brainly.com/question/11352260

#SPJ1

5 0
2 years ago
3. A security system uses sensors at every door and window which will set off analarm if any one of them is opened. There is als
sp2606 [1]
A combination circuit would be needed for security because if you had a strait circuit if any one of those sensors or the keypad was ripped off the circuit will now let the flow of electricity be possible but with a combination circuit it helps eliminate that problem because even if on flow path is cut off, other paths are still available <span />
6 0
3 years ago
What is MARC? Discuss structure of MARC
nordsb [41]
What do you mean? I don't understand.
7 0
3 years ago
Other questions:
  • Write a program to read as many test scores as the user wants from the keyboard (assuming at most 50 scores). Print the scores i
    13·1 answer
  • A viral video is one that can infect your computer with malware. <br> a. True <br> b. False
    6·1 answer
  • Who was the 1st person to use the term television
    11·1 answer
  • Type the correct answer in the box. Spell all words correctly.
    11·1 answer
  • Escribe, en los siguientes cuadros, los conceptos que correspondan
    10·1 answer
  • In rolling a die four times, what is the odds that at least one 5 would appear?
    6·1 answer
  • A. Calculate the diffusion coefficient for magnesium in aluminum at 450°C.
    14·1 answer
  • What malware looks like a useful or desired executable program but is, in reality, a program that is supposed to cause harm to y
    11·1 answer
  • Select the correct answer.
    11·1 answer
  • Which one is better AMD Ryzen 5 Or Nvidia Gtx 2080 ti
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!