Question

The list given has values [1, 3, 2, 7]. After calling list.duplicate(), the list should store...

The list given has values [1, 3, 2, 7].
After calling list.duplicate(), the list should store the values [1, 1, 3, 3, 2, 2, 7, 7].

public class ArrayIntList {
private int[] elementData;
private int size = 0;

// Doubles-up each element in this list
public void duplicate() {
// TODO:
}
}

Answer in Java/C# only

Homework Answers

Answer #1
public class ArrayIntList {
    private int[] elementData;
    private int size = 0;

    // Doubles-up each element in this list
    public void duplicate() {
        int[] result = new int[elementData.length * 2];
        int index = 0;
        for (int i = 0; i < result.length; i += 2) {
            result[i] = elementData[index];
            result[i+1] = elementData[index];
            ++index;
        }
        elementData = result;
        size *= 2;
    }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Java Program: You will be inserting values into a generic tree, then printing the values inorder,...
Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...
Identify errors and correct them of the following Java program: 1. import java.utility.Random; 2. 3. public...
Identify errors and correct them of the following Java program: 1. import java.utility.Random; 2. 3. public class Sequen 4. 5. private int stand 6. private int calc; 7. 8. public Sequen(int numStand) 9. { 10. stand = numStand; 11. skip; 12. } 13. 14. public void skip() 15. { 16. Random sit = new Random(); 17. calc = sit.nextInt(stand) + 1; 18. } 19. 20. public getStand() 21. { 22. return stand; 23. } 24. 25. int getCalc() 26. {...
Identify errors and correct them of the following Java program: 1. import java.utility.Random; 2.   3. public...
Identify errors and correct them of the following Java program: 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand   6. private int calc;   7.   8. public Sequen(int numStand) 9. { 10.         stand = numStand; 11.         skip; 12.         } 13.           14.         public void skip() 15.         { 16.         Random sit = new Random(); 17.         calc = sit.nextInt(stand) + 1; 18.         } 19.           20.         public getStand() 21.         { 22.         return stand; 23.         } 24.           25.         int getCalc() 26.         { 27.         return calc; 28.         } 29.         } One error is already identified for you as shown below:...
URGENT!!!!!!!!!!!!!!!!!!!!! Identify errors and correct them of the following Java program: 1. import java.utility.Random; 2.   3....
URGENT!!!!!!!!!!!!!!!!!!!!! Identify errors and correct them of the following Java program: 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand   6. private int calc;   7.   8. public Sequen(int numStand) 9. { 10.         stand = numStand; 11.         skip; 12.         } 13.           14.         public void skip() 15.         { 16.         Random sit = new Random(); 17.         calc = sit.nextInt(stand) + 1; 18.         } 19.           20.         public getStand() 21.         { 22.         return stand; 23.         } 24.           25.         int getCalc() 26.         { 27.         return calc; 28.         } 29.         } One error is already identified for you as shown...
Identify errors and correct them of the following Java program: 1. import java.utility.Random; 2.   3. public...
Identify errors and correct them of the following Java program: 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand   6. private int calc;   7.   8. public Sequen(int numStand) 9. { 10.         stand = numStand; 11.         skip; 12.         } 13.           14.         public void skip() 15.         { 16.         Random sit = new Random(); 17.         calc = sit.nextInt(stand) + 1; 18.         } 19.           20.         public getStand() 21.         { 22.         return stand; 23.         } 24.           25.         int getCalc() 26.         { 27.         return calc; 28.         } 29.         } One error is already identified for you as shown below:...
Identify errors and correct them of the following Java program: 1. import java.utility.Random; 2.   3. public...
Identify errors and correct them of the following Java program: 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand   6. private int calc;   7.   8. public Sequen(int numStand) 9. { 10.         stand = numStand; 11.         skip; 12.         } 13.           14.         public void skip() 15.         { 16.         Random sit = new Random(); 17.         calc = sit.nextInt(stand) + 1; 18.         } 19.           20.         public getStand() 21.         { 22.         return stand; 23.         } 24.           25.         int getCalc() 26.         { 27.         return calc; 28.         } 29.         } One error is already identified for you as shown below:...