Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is the exercise.

In this exercise you’ll get a chance to improve our expanding array to add some of the other functionality that you get in ArrayList.

You should add three methods:

public void add(int index, int element)
public int remove(int index)
public int size()

//////////////////////////////////////////////////////////////////////////////////////

This is the code:
Java
public class ExpandingArray
{
    private static final int STARTING_SIZE = 10;
    private int[] arr;
    private int currentSize;
    private int numElements;
    
    public ExpandingArray()
    {
        arr = new int[STARTING_SIZE];
        currentSize = STARTING_SIZE;
        numElements = 0;
    }
    
    // Remove the element at index `index` and shift
    // all subsequent elements to the left. 
    public int remove(int index)
    {
        // your code here
        return 0;
    }
    
    // Add the int `element` at the `index` in the array.
    // You'll need to shift everything one index to the right
    // after this index.
    public void add(int index, int element)
    {
        // your code here   
    }
    
    // Return the number of elements in your array.
    public int size()
    {
        // your code here
        return 0;
    }
    
    private boolean isFull()
    {
        return numElements == currentSize;
    }
    
    private void expand()
    {
        System.out.println("Expanding");
        int newSize = currentSize * 2;
        int[] newArray = new int[newSize];
        
        // Copy over old elements
        for(int i = 0; i < currentSize; i++)
        {
            newArray[i] = arr[i];
        }
        
        currentSize = newSize;
        arr = newArray;
    }
    
    public int get(int index)
    {
        return arr[index];
    }
    
    public void add(int x)
    {
        if(isFull())
        {
            expand();
        }
        arr[numElements] = x;
        numElements++;
    }
    
    public String toString()
    {
        String str = "{";
        for (int i=0; i < numElements; i++) {
            str += arr[i] + ", ";
        }
        if (str.length() > 0 && str.charAt(str.length()-2)==',') {
            str = str.substring(0, str.length()-2);
            str += "}";
        }
        return str;
    }
}


What I have tried:

I havent tried anything.
Posted
Updated 11-Jan-18 17:56pm
v4

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
So start by looking at what that does, and compare that to an ArrayList. That should tell you what you are missing and what you should add.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Member 13618455 11-Jan-18 11:57am    
Our teacher cant do it either. :(
OriginalGriff 11-Jan-18 12:07pm    
Then you need a new teacher...
Quote:
I havent tried anything.
Then try something.

For instance, the size method implementation is trivial.
The other two methods, namely add and remove are more difficult, anyway you know, specific question are welcomed here.
 
Share this answer
 
Comments
OriginalGriff 11-Jan-18 12:28pm    
He did try something.
CTRL+A, CTRL+C, ALT+TAB, CTRL+V, click the post button.
That kind of effort can exhaust a student you know - they are delicate types.
CPallini 11-Jan-18 12:40pm    
I am too old fashioned to appreciate such, admittetly remarkable, efforts.
Quote:
I havent tried anything.

First thing to try is read documentation.
Quote:
Our teacher cant do it either.

Then your teacher is not qualified.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900