【问题标题】:How to delete elements from an array?如何从数组中删除元素?
【发布时间】:2014-03-20 01:54:28
【问题描述】:

如何删除给定索引处的整数以及如何压缩myInts

这是我得到的,但我一直收到错误。

public void deleteInt(int index) {

    int[] newInts = Arrays.copyOf(myInts, myInts.length);

    if (myInts[index] != 0) {
        myInts[index] = 0;

        for (int i : myInts) {
            if (myInts[i] != 0) {
                newInts[i] = myInts[i];
            }
        }
    }
    myInts = newInts;
    currentInt++;
}

这是我得到的错误:

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 11

【问题讨论】:

  • 为什么投反对票?这似乎是一个合理的问题,只是需要更多细节。
  • @DonBranson 猜他们只是不知道该怎么做,所以他们投了反对票,但无论如何,你知道 id 如何在不使用数组列表的情况下处理它吗?
  • 我认为 ArrayList 将是一个好方法。有理由不这样做吗?
  • 因为这里没有明显的问题。 “删除整数”是什么意思?这是否意味着从内存中删除整数并释放它所占用的空间?这是否意味着将数组中的所有其他整数向下移动一个索引?此外,仅仅说“不断出错”并不是特别有用。
  • @SimonC,他在您的评论之前添加了具​​体错误。 :) 此外,他的问题并没有说“删除一个整数”,而是说“删除给定索引处的整数”,这很清楚。

标签: java arrays


【解决方案1】:

为此,您应该使用ArrayList 之类的东西。使用Array 会导致您编写类似于示例中的代码,从质量和性能的角度来看,这通常是一个非常糟糕的主意。

编辑:见下面的代码:

ArrayList<int> ret = new ArrayList<int>(Arrays.asList(myInts));
ret.remove(index);
return ret.toArray();

【讨论】:

  • 我知道如何使用arraylist,我需要使用常规数组,因为它是被要求做的。
【解决方案2】:

所以,首先是单元测试:

package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Arrays;

import static org.junit.Assert.assertTrue;

@RunWith(MockitoJUnitRunner.class)
public class ArrayExample_UT {

    @InjectMocks
    private ArrayExample subject;

    int[] testArray = {1, 2, 3, 4, 5};
    int[] expectedArray = {1, 2, 4, 5};

    @Test
    public void testThat_RemoveEntry_RemovesCorrectEntry() throws Exception {
        assertTrue(Arrays.equals(expectedArray, subject.removeEntry(2, testArray)));
    }
}

然后,一些绿色运行的代码:

package com.example;

class ArrayExample {

    public int[] removeEntry(int skipIndex, int[] sourceArray){
        int[] newArray = new int[sourceArray.length - 1];
        int targetIndex = 0;
        for(int i = 0; i < sourceArray.length; i++){
            if(i != skipIndex){
                newArray[targetIndex++] = sourceArray[i];
            }
        }
        return newArray;
    }
}

【讨论】:

  • 感谢您的所有帮助,感谢您一直以来的帮助,但在看到您的帖子之前,我得到了其他人的回答,抱歉
  • +1 用于给出生产质量的答案,包括单元测试!
【解决方案3】:

此代码未正确使用增强的 for 循环:

    for (int i : myInts) {
        if (myInts[i] != 0) {
            newInts[i] = myInts[i];
        }
    }

它试图从 myInts 中读取元素 i,但 i 是元素的内容,而不是它的索引。因此,一旦某个元素包含一个值 > 数组的长度,就会出现 outofbounds 异常。

public void deleteInt(int index) {

    // can be 1 element shorter as we are going to erase 1 element
    // also, copying the contents of the original array in is a waste of time
    // so we just create it.
    int[] newInts = new int[myInts.length-1]; 

    // the easiest way is to use an extra variable to track the insertions in the
    // new array
    int j=0;
    for (int i=0; i < myInts.length; i++) {
        if (i != index) {
            newInts[j++] = myInts[i];
        }
    }

    // so now we have a new shortened copy of the array, but as the function is void,
    // its life ends here :)
}

【讨论】:

  • 这只是将索引复制到末尾但不会删除它
  • 如果我有数组 1,2,3,4,5 并执行 deleteInt(3);我应该得到 1,2,3,5
  • 编辑后的版本可以满足您的需求,并且还指出了您的代码存在的问题 - 没有解决它,因为您刚刚承认这是家庭作业 :)
  • 谢谢它完美运行,我解决了剩下的问题。感谢您的所有帮助
  • 不客气!现在,也许您可​​以稍微扩展一下练习以保持它的趣味性,并调整我的代码以进行适当的删除,即,在不使用 newInts 的情况下创建 myInts 的缩短版本。祝你好运!
猜你喜欢
  • 1970-01-01
  • 2021-10-23
  • 2019-04-09
  • 2013-09-01
  • 2020-04-16
  • 1970-01-01
  • 2011-10-31
相关资源
最近更新 更多