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
Which of the following shows data conversion taking place?
PSYCHO15rus [73]

Answer:

C

data converted from double to integer.

7 0
2 years ago
Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the elemen
Verdich [7]

Answer:

Replace <STUDENT CODE> with

for (i = 0; i < SCORES_SIZE; ++i) {

       if(lowerScores.at(i)<=0){

           lowerScores.at(i) = 0;

       }

       else{

           lowerScores.at(i) = lowerScores.at(i) - 1;

       }  

   }

Explanation:

To do this, we simply iterate through the vector.

For each item in the vector, we run a check if it is less than 1 (i.e. 0 or negative).

If yes, the vector item is set to 0

If otherwise, 1 is subtracted from that vector item

This line iterates through the vector

for (i = 0; i < SCORES_SIZE; ++i) {

This checks if vector item is less than 1

       if(lowerScores.at(i)<1){

If yes, the vector item is set to 0

           lowerScores.at(i) = 0;

       }

       else{

If otherwise, 1 is subtracted from the vector item

           lowerScores.at(i) = lowerScores.at(i) - 1;

       }  

   }

Also, include the following at the beginning of the program:

<em>#include <vector></em>

8 0
3 years ago
When might an lcd monitor experience distorted geometry?
horsena [70]

The time when an LCD monitor is experiencing distorted geometry is when there is a presence of the screen into having a display that is not set on the resolution that is supposed to be in native, this is indicative that it is experiencing distorted geometry.

7 0
3 years ago
if a person walks 10 metres due to south in 10 seconds 10 due east in the next 10 seconds and transmitted you're not in the next
Scorpion4ik [409]

Answer:

\frac{1}{3}\ m/s

Explanation:

The computation of the velocity by using the following formula is

As we know that

Velocity = \frac{Displacement}{Total\ time\ taken}

where,

Displacement is 10 meters

Total time taken is 30 seconds

Now placing these values to the above formula

So, the velocity of Dash is

= \frac{10\ meters}{30\ seconds}

= \frac{1}{3}\ m/s

We simply applied the above formula so that we can get the velocity and the same is to be considered

5 0
2 years ago
What allows a programmer to write code quickly and efficiently for an action that must be repeated?
Marta_Voda [28]
An literation :))!! i hope u get it right lol
6 0
2 years ago
Read 2 more answers
Other questions:
  • Which game would be classified as an educational game
    14·1 answer
  • How can development in ICT be utilized to speed up the development and integration efforts
    15·1 answer
  • Which type of system would be more reliable for keeping a plane traveling at constant speed – an automatically controlled feedba
    11·1 answer
  • The function below takes a single parameters: a list of numbers called num_list. Return a new list containing just the three lar
    5·1 answer
  • In bankruptcy, most of a debtor’s assets will probably be used to repay unsecured debt
    9·1 answer
  • How should this be accomplished? Business users have requested that the Salesforce Administrator allow agents to view a list of
    6·1 answer
  • Composition means ________. a)that data fields should be declared private b)that a class extends another class c)that a variable
    6·1 answer
  • How would you justify using cloud computing?
    12·1 answer
  • To insert text from a separate file into your Word document
    10·1 answer
  • When defining a system
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!