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
Select the correct answer from each drop-down menu.
tia_tia [17]

Answer:

<dd> tag is used

Explanation:

The <dd> tag is used in HTML document to explain set of terms. The <dd> tag list is used in conjunction with the <dl> term. Inside a <dd> tag we can insert text, sentence, paragraph or links. There are 4 primary tags to build any website. Every HTML document begins and ends with HTML tag.

6 0
3 years ago
Sketch a 2K x 32 memory built from 1K x 8 memory chips. Include control, address, and data line details, as well as any decoding
Scrat [10]

Answer:

See my explanations and attachment

Explanation:

Construct an 8k X 32 ROM using 2k X 8 ROM chips and any additional required components. Show how the address and data lines of the constructed 8k X 32 ROM are connected to the 2k X 8 chips.

I tried to solve it but I am not sure if I got the correct answer. Could anyone check my drawing and correct me?

8 0
3 years ago
You are asked to write an app to keep track of a relatively small music library. The app should load song information from a dat
gogolik [260]

Answer:

C++ PROGRAM

Explanation:

6 0
3 years ago
Which type of electronic community allows real time discussion among members
GrogVix [38]

Answer:

Instant messaging is the type of electronic community that allows real-time discussion among members.

7 0
3 years ago
Define a pointer variable named daco that can be used for objects of the class Banana.
ozzi

Answer:

c)Banana * daco;

Explanation:

To declare an variable pointer we use * symbol after writing it's type.For example int *.Then we write the name of the variable since the name of the variable is daco.The class is a user defined data type so instead of writing any data type we will write class name then the * then name of the variable.

Banana * daco; which matches the option c Banana* daco;

4 0
3 years ago
Read 2 more answers
Other questions:
  • The Cisco IOS automatically modifies the dead interval when the _____ interval is changed. (Points : 2) hello
    13·1 answer
  • William found out that someone used his report on American culture without his permission. What is William a victim of? A. plagi
    7·2 answers
  • Anyone know how to delete that I need ASAP
    13·1 answer
  • -(-13) P binary using signed. 2's complement representation
    11·1 answer
  • I need a utube name for a gaming channel....PLEASE HELP...BRAINLIEST
    5·1 answer
  • I'm trying to move the figure a little away from, the column line and every time I move it and click ok it goes back to being be
    11·1 answer
  • What is a programming concept based on objects and data and how they relate to one another, instead of logic and actions. Hint:
    10·1 answer
  • Help me and i'll mark brainliest
    9·1 answer
  • Give 3 reasons why it is believed that smart phones precent us from communicating face to face.give three reasons why it is beli
    8·1 answer
  • What happens if part of an ftp message is not delivered to the destination?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!