从数组中移除一个元素,而不使用列表。

huangapple go评论75阅读模式
英文:

Removing an element from an array without using list

问题

我一直在努力编写一个方法,通过创建一个新数组,将其大小减小1,并将除了“position”位置上的项目之外的所有内容复制过去,以便从数组中删除项目。有什么原因导致我的代码不起作用吗?不过我不能使用列表函数,必须使用数组。

int newArray[] = new int[originalArray.length - 1];

for (int i = 0; i < originalArray.length; i++) {
    if (i < position)
        newArray[i] = originalArray[i];
    else if (i > position)
        newArray[i - 1] = originalArray[i];
}
英文:

I've been trying for ages to make a method that lets me remove an item from an array by making a new array, reducing it's size by 1, and then copying everything over except for the item in "position". Any reason why my code wouldn't be working? I can't use the list function though, I have to use arrays.

int newArray[] = new int[originalArray.length - 1];

for (int i = 0; i &lt; originalArray.length; i++) {
    if (i &lt; position)
        newArray[i] = originalArray[i];
    else if (i == position)
        i++;
    else if (i &gt; position)
        newArray[i] = originalArray[i];
}

答案1

得分: 1

你不应该尝试编写超出范围的元素,尝试这样做:

int newArray[] = new int[originalArray.length - 1];

for (int i = 0; i < newArray.length; i++) {
    if (i < position)
        newArray[i] = originalArray[i];
    else
        newArray[i] = originalArray[i+1];
}
英文:

You shouldn't try to write element out of range, try this:

int newArray[] = new int[originalArray.length - 1];

for (int i = 0; i &lt; newArray.length; i++) {
    if (i &lt; position)
        newArray[i] = originalArray[i];
    else
        newArray[i] = originalArray[i+1];
}

答案2

得分: 1

public static int[] removeElement(int[] originalArray, int position) {
    int newArray[] = new int[originalArray.length - 1];

    for (int i = 0; i < originalArray.length; i++) {
        if (i < position)
            newArray[i] = originalArray[i];
        else if (i > position)
            newArray[i - 1] = originalArray[i];
    }

    return newArray;
}

// P.S. I reccomend you to use `System.arraycopy()`:

public static int[] removeElement(int[] arr, int pos) {
    if (pos < 0 || pos >= arr.length)
        return arr;
    if (arr.length == 1)
        return new int[0];

    int[] res = new int[arr.length - 1];
    System.arraycopy(arr, 0, res, 0, pos);
    System.arraycopy(arr, pos + 1, res, pos, arr.length - pos - 1);
    return res;
}

// Output:

int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] res = removeElement(arr, 4);
System.out.println(Arrays.toString(res));   // [0, 1, 2, 3, 5, 6, 7, 8, 9]
英文:
public static int[] removeElement(int[] originalArray, int position) {
    int newArray[] = new int[originalArray.length - 1];

    for (int i = 0; i &lt; originalArray.length; i++) {
        if (i &lt; position)
            newArray[i] = originalArray[i];
        else if (i &gt; position)
            newArray[i - 1] = originalArray[i];
    }

    return newArray;
}

P.S. I reccomend you to use System.arraycopy():

public static int[] removeElement(int[] arr, int pos) {
    if (pos &lt; 0 || pos &gt;= arr.length)
        return arr;
    if (arr.length == 1)
        return new int[0];

    int[] res = new int[arr.length - 1];
    System.arraycopy(arr, 0, res, 0, pos);
    System.arraycopy(arr, pos + 1, res, pos, arr.length - pos - 1);
    return res;
}

Output:

int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] res = removeElement(arr, 4);
System.out.println(Arrays.toString(res));   // [0, 1, 2, 3, 5, 6, 7, 8, 9]

huangapple
  • 本文由 发表于 2020年10月24日 06:31:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64508056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定