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
Three IT Companies. Help me please!
GuDViN [60]

Answer:

Apple, Samsung and Foxconn

Explanation:

They are the leading it companies today

3 0
3 years ago
Read 2 more answers
Explain the role of ICT in banks​
vladimir1956 [14]

Answer: ICT help banks improve the efficiency and effectiveness of services offered to customers, and enhances business processes, managerial decision making, and workgroup collaborations, which strengthens their competitive positions in rapidly changing and emerging economies.

Explanation: please give branliest I only need one more to make ace

7 0
3 years ago
How do I turn on your asus chromebook flip?
galben [10]
I believe the charger has to be connected for it to turn on.
3 0
3 years ago
A programming language that captures only the most primitive operations available to a machine. Anything that a computer can do
Anton [14]

Answer:

A. True.

Explanation:

Programming languages are computer based languages used to pass instructions or task that can be interpreted to machine language, which can be understood by the computer system. Low level programming language and high level programming language are the two categories of program languages.

Low level languages use low level commands. They use processor instruction sets and primitive opcodes for their instruction syntax. Example of a low level language is Assembly language.

High level languages are English based and more complex in its compilation.

8 0
4 years ago
What are the three main desktop operating systems used today
Norma-Jean [14]

Answer:The three most common operating systems for personal computers are Microsoft Windows, macOS, and Linux.

Explanation:

3 0
2 years ago
Other questions:
  • The main differences between laptops and desktop computers other than size and portability.
    14·1 answer
  • What are the Positive and negative sites of the internet
    14·1 answer
  • Juliet is trying to increase her savings account and she decides that she is going save an additional $700 every month. If her m
    9·1 answer
  • How are modern computers different from eniac?
    5·1 answer
  • write a function that given an integer Y and 3 non-empty string A,B,W, denotingthe year of vacations, the beginning month, the e
    5·1 answer
  • Which one of the following items is an example of software?
    5·1 answer
  • Help me or I'll go insane I've 6x already I'm about to lose it!!!!
    11·2 answers
  • [1] Our son has started playing organized T-ball, a beginner’s version of baseball. [2] “Organized” is what parents call it, any
    9·2 answers
  • If you use your smartphone as a hotspot to connect to the Internet on your tablet you are using a ________.quillet
    11·1 answer
  • Convert<br> 0.625 to binary
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!