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
What is a protein called if it loses its shape?
hichkok12 [17]
When a protein is denatured, itloses its function. Examples: A denatured enzyme ceases to function. A denatured antibody no longer can bind its antigen.
7 0
3 years ago
4. In Drag and Drop method of Excel , to copy the data , you need to press __________ key while dragging the cells.
alexgriva [62]

Answer:

The <u>control</u> [Ctrl] key

Explanation:

While holding down left click and pressing control key it will copy to where you drag and drop it.

6 0
3 years ago
Where are the instructions stored that the computer needs when it is switched on?
WARRIOR [948]
This is stored in the ROM (Read Only Memory)
I believe that is the answer, all i can come up with.
4 0
3 years ago
Given that Jamie worked 50 hours (Hours = 50) last week and earns $10.00 an hour (Rate = 10), how much did Jamie earn last week,
quester [9]

Answer:

<em>Jamie earned (Total pay)  $500.</em>

<em></em>

Explanation:

We are given the following code:

<em>If (Rate >=10) OR (Hours <=40) Then </em>

<em>    TotalPay = Hours * Rate </em>

<em>Else </em>

<em>    TotalPay = (Hours * Rate)+(Hours–40)*Rate*1.5 </em>

<em>End If</em>

<em />

Let us understand the code line by line:

The first line contains an if statement with 2 conditions:

i.e. 1st condition:

The rate is greater than or equal to 10

2nd condition:

Number of hours are lesser than or equal to 40.

There is OR between the two condition i.e. the statement next to if() statement will get executed if any one of them becomes true and else part will not be executed.

The next statement is:

TotalPay = Hours * Rate

It calculates the pay if any of the two conditions written earlier becomes true.

Next statement is else statement:

It will get executed given that the above if() statement becomes false.

Now, we are given that Jamie worked 50 hours last week and earns $10.00 an hour:

i.e.

Hours = 50

Rate = 10

Now, let get to the code execution.

The first condition is true i.e. Rate >= 10 (because Rate is 10 here)

So, the following statement will be used to calculate the Total pay:

TotalPay = Hours * Rate

and else part will not be executed.

TotalPay = 50 * 10  =<em> $500  </em>

<em></em>

<em>Jamie earned (Total pay)  $500.</em>

3 0
3 years ago
I can’t unblock brainly from my computer, I think a robot has the same IP address .
prisoha [69]
They won’t let it be on your computer, knowing it well give you answers
6 0
3 years ago
Read 2 more answers
Other questions:
  • Jason is creating a webpage on the basic parts of a camera. He has to use a mix of both images and content for the web page to h
    11·1 answer
  • If you are trying to create a web page for your band and having difficulty creating links to other groups on your page, what is
    7·1 answer
  • Write a Java program to print the result in the series 10, 15, 20, 25, ..., 50. Hint: You can use an iteration statement for wri
    9·1 answer
  • What are examples of educational obstacles to a career plan?
    13·1 answer
  • Which of the following would be considered a primary source of information?
    6·1 answer
  • Fill the validateForm function to check that the phone number contains a number (use the isNaN function) and that the user name
    8·1 answer
  • Arrange the steps below to outline what maia needs to do to accomplish this task.​
    9·1 answer
  • PLEASE HELP! I'm offering brainliest!
    6·1 answer
  • Java code?????
    12·1 answer
  • when you insert a new column, the existing columns are shifted to the ____ and the new column has the same width as the column d
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!