Click here to Skip to main content
15,920,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I feel a bit embarrassed to ask this because this is the first problem I have been actually stumped with over a long long amount of time... Here is the code:

public class ExpandingArray
{
    private static final int STARTING_SIZE = 10;
    private int[] arr;
    private int[] tempArr; 
    private int[] finalArr; 
    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)
    {
        tempArr = new int[10]; 
        for(int i = 0; i < index; i++)
        {
            tempArr[i] = arr[i];
        }
        for(int i = index; i < arr.length - 1; i++)
        {
            tempArr[i] = arr[i + 1];
        }
        arr = new int[9];
        for(int i = 0; i < arr.length; i++)
        {
            arr[i] = tempArr[i];
        }
        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)
    {
        int [] tempArr = new int[arr.length + 1];
        for(int i = 0; i < index; i++)
        {
            tempArr[i] = arr[i];
        }
        tempArr[index] = element; 
        
        for(int k = index + 2; k <= tempArr.length; k++)
        {
            tempArr[k-1] = arr[k - 2];
        }
        
        int [] finalArr = new int[tempArr.length + 1];
        for(int i = 0; i < tempArr.length; i++)
        {
            finalArr[i] = tempArr[i];
        }
        finalArr[finalArr.length - 1] = arr[arr.length - 1];
        numElements = finalArr.length; 
        currentSize = finalArr.length; 
        arr = finalArr; 
 
    }
    
    // Return the number of elements in your array.
    public int size()
    {
        return arr.length;
    }
    
    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()
    {
        // Return empty curly braces if the array is empty
        if (numElements == 0)
        {
            return "{}";
        }
        
        // Return Elements 
        String str = "{";
        for (int i=0; i < numElements - 1; 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:

The methods remove, add and size are the ones that are being submitted. I made remove and size work but for the life of me I cant make add work. Whenever I complied it, I would be missing the last term of the array. Whenever I modify the existing code and tried to change the values length, I would get an out of bounds exception or a random index would repeat itself 3 times! I tried creating a third array as well and increasing the index by one, but that resulted in errors that didn't exist before... I am incredibly frustrated, please help!
Posted
Updated 28-Sep-21 5:20am

1 solution

When you reach this point the value of i should be the same as index, so the next element to copy is at index (or i) + 1 rather than 2
Java
for(int k = index + 1; k < tempArr.length; k++)
{
    tempArr[k] = arr[k - 1];  // temparray item indices are 1 greater than the original array
}

I am not sure why you need the following code, since temparray is (or should be) already complete.
Java
int [] finalArr = new int[tempArr.length + 1];
for(int i = 0; i < tempArr.length; i++)
{
    finalArr[i] = tempArr[i];
}
finalArr[finalArr.length - 1] = arr[arr.length - 1];


[edit]
The following code worked for my test:
Java
for (int i = 0; i < index; ++i) {
    finalArr[i] = arr[i]; // copy elements before index
}
finalArr[index] = element; // insert the new element

for (int i = index + 1; i < finalArr.length; ++i) {
    finalArr[i] = arr[i - 1]; // copy the elements after index
}

[/edit]
 
Share this answer
 
v3

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