gpt4 book ai didi

java - 如何在 Java 中删除 Array Generic 对象 Sack

转载 作者:行者123 更新时间:2023-12-02 01:37:46 25 4
gpt4 key购买 nike

因此,我正在创建一个名为“Sack”的通用数据结构。在此,我将元素添加到麻袋中,抓取随机元素,查看它是否为空,或转储其内容等。此外,我还创建它来扩展以容纳所需数量的元素。

删除辅助方法应该删除底层数据结构中的项目指定索引。它应该通过将其替换为底层数组中的“最后一个”元素来实现。它还应该确保任何未使用的元素都无效。

目前,我正在研究删除方法,但我的删除方法遇到了麻烦。当我运行测试时,我收到错误,说它没有正确的修饰符。因此我的代码是

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;
}

我在此处删除时收到错误

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

它期望接收的值是 2,但它接收的值是 1。请下拉确保这一点的任何方法。

这是我的测试。我无法修改我的测试,只能修改我的代码。我在第一行收到错误。我会评论一下,让你看看。

@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");
}
}

最佳答案

这将保持顺序:

public void remove(final int index) {

if (index >= size || 0 > index) {

// TODO better to throw 'new ArrayIndexOutOfBoundsException(index)'
// when remove illegal index
return;
}

if (index < size - 1) { // move whole array

System.arraycopy(
elementData, index + 1, // copy from
elementData, index, // copy to
size - index - 1); // copy length
}

elementData[--size] = null;
}

关于java - 如何在 Java 中删除 Array Generic 对象 Sack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54973786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com