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
Name this<br><br>The feature allows you to add text/picture in the background of the document. ​
sertanlavr [38]
I think it is picture/text wrapping

I wouldn’t trust this though

I’m sorry if it’s wrong
4 0
3 years ago
What describes the current cloud landscape for business?
KonstantinChe [14]

Answer:

Organizations need to change the vast majority of their tasks to the cloud to stay competitive describes the current Cloud landscape for business

Explanation:

All cloud computing frameworks are effectively open over the web with minimal requirement for actual equipment other than cloud servers at the back end.

5 0
2 years ago
Compare and contrast the various options that exist for acquiring software.
Softa [21]

Answer:

   The various options that exist for acquiring software are:

  •  By developing a various custom applications so that, they can satisfied the requirements in an organization.
  •  By purchasing various software application or packages for modify the software to meet the specific requirements.
  •  Used the open source software for developing the specific information system so that, the system are acquired.
  •  Developed the in-house capabilities and skills of employee, this is the main advantage for acquiring the software.  
6 0
3 years ago
How do I change my username/display name
exis [7]

Answer:

.

Explanation:

you do to me on the bottom bar and once you hit that you click the pencil and change it

6 0
3 years ago
Read 2 more answers
Which statement is true about a metamorphic rock that was exposed to stress equally from all directions?
Zolol [24]
I think the correct answer from the choices listed above is option B. A  metamorphic rock that was exposed to stress equally from all directions <span>will have all of its mineral crystals aligned in layers. Hope this answers the question. Have a nice day.
</span>
7 0
3 years ago
Other questions:
  • The network board in a workstation is currently configured as follows:- network speed = auto- duplexing = autoThe workstation is
    13·2 answers
  • Dialogue is not a characteristic of functional text because
    9·1 answer
  • Imagine that you are preparing a series of bitmap graphics for a Website. To decrease the download time for each graphic, you ca
    5·1 answer
  • Which of the following is a negative impact of technology on society
    9·1 answer
  • ​Microsoft claims that the microsoft project software can _____.
    11·1 answer
  • Secops focuses on integrating the need for the development team to provide iterative and rapid improvement to system functionali
    14·1 answer
  • A demarc is the point in a telephone network where the maintenance responsibility passes from a telephone company to the subscri
    10·1 answer
  • In general, what are reasons that someone would choose to use lossy compression? Write a brief response below including at least
    10·1 answer
  • Do you all like IXL?
    8·1 answer
  • FREEEEE 100 POINTS COME TAKE COME COME COME<br><br> only if u like da black panther
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!