Try to search your question here, if you can't find : Ask Any Question Now ?

How to remove an object Sack implementation in Java

HomeCategory: stackoverflowHow to remove an object Sack implementation in Java
Avatarcraig asked 5 months ago

So, I am creating a generic data structure named “Sack”. In this I add items to a sack, grab a random item, see if it’s empty, or dump out its contents etc. Also I’m creating it to expand to hold as many items as needed.

The remove helper method should remove the item in the underlying data structure at the
specified index. It should do so by replacing it with the “last” element in the underlying array.
It should also ensure that any unused elements are nullified.

Currently, I’m working on the remove method and I’m having troubles on my remove method. I am receiving errors when I run my tests saying it does not have the correct modifiers. Therefore my code is

public class Sack<E>
{
public static final int DEFAULT_CAPACITY = 10;
private E [] elementData;
private int size;

@SuppressWarnings("unchecked")
public Sack()
{
    elementData = (E[]) new Object[DEFAULT_CAPACITY];
}
@SuppressWarnings("unchecked")
public Sack(int capacity)
{
    if(capacity < 0)
    {
        throw new IllegalArgumentException("capacity " + capacity);
    }
    this.elementData = (E[]) new Object[capacity];
}

public boolean isEmpty()
{
    if(size == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
public void add(E item)
{
    int index = size++;
    if(size >= elementData.length-1)
    {
        elementData = Arrays.copyOf(elementData, size);
    }
    elementData[index] = item;
}
  public E [] dump()
{
   E [] E2 = Arrays.copyOf(elementData, size);
   for(int i = 0; i < size; i++)
   {
      elementData[i] = null;

   }
   size = 0;
    return E2;
}

I receive the error in my remove right here

 public void remove(int index)
{
    for (int i = index; i < size; i++)
    {
        elementData[i] = elementData[i + 1];
    }
    size--;
}
}

The value it is expecting to receive is 2, however what it’s receiving is 1. Drop down any ways of ensuring this.

So here are my tests. I cannot modify my tests, only my code.
I receive an error on the first line. I will comment it, so you can see.

@Test
public void testRemove()
{
    assertEquals(2, remove.getModifiers(), "remove does not have the correct modifiers"); // I receive an error here. 
    try
    {
        Random rand = new Random();
        Integer[] setElementData = new Integer[10];
        ArrayList<Integer> expectedElements = new ArrayList<Integer>();
        int randElement;
        for(int i=0; i<10; ++i)
        {
            randElement = rand.nextInt(50) + 1;
            setElementData[i] = randElement;
            expectedElements.add(randElement);
        }
        elementData.set(s, setElementData);
        size.set(s, 10);

        int randIndex;
        int numEe;
        while(!expectedElements.isEmpty()) {
            numEe = expectedElements.size()-1;
            randIndex = rand.nextInt(expectedElements.size());
            expectedElements.set(randIndex, expectedElements.get(numEe));
            expectedElements.remove(numEe);
            remove.invoke(s, randIndex);
            assertEquals(expectedElements.size(), size.get(s), "remove is not working correctly (check size usage)"); //Error now takes place here. 
            for(int i=0; i<expectedElements.size(); ++i) {
                assertEquals(expectedElements.get(i), ((Object[])elementData.get(s))[i], "remove is not working correctly (sack array element order incorrect)");
            }
            assertNull(((Object[])elementData.get(s))[expectedElements.size()], "remove is not working correctly (sack array element not nullified correctly)");
        }
        assertEquals(0, size.get(s), "remove is not working correctly (check size usage)");

    } catch (Exception e) {
        fail("remove is not working correctly");
    }
}
1 Answers
Best Answer
AvatarFernando answered 5 months ago
Your Answer

14 + 19 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com