简体   繁体   English

从数组中删除元素,然后减小数组大小的最有效方法

[英]Most efficient way to remove an element from an array, then reduce the size of the array

Say I have an array of 10 elements. 假设我有10个元素的数组。 Another part of my program determines I must remove the item at index 4. 程序的另一部分确定我必须删除索引4处的项目。

What is the most efficient method to remove the item and shorten the array? 删除项目并缩短阵列的最有效方法是什么?

I wrote the following method, however it does not seem to work properly. 我编写了以下方法,但是它似乎无法正常工作。 Am I missing something, for example if the index to remove is 0? 我是否缺少某些内容,例如要删除的索引是否为0? The method is called by sending an array and the index to be removed. 通过发送数组和要删除的索引来调用该方法。

I realize there are Array Lists and other types of lists. 我意识到有数组列表和其他类型的列表。 However this is an assignment for a programming course and MUST use ARRAYs. 但是,这是编程课程的作业,必须使用阵列。

//Removes the index from the array and returns the array.
    NumberTile[] removeAndTrim(NumberTile[] array, int index){
        NumberTile[] save = array;
        array = new NumberTile[save.length-1];
        for (int i=0; i<index; i++){
            array[i]=save[i];
        }//end for loop
        for (int j=index; j<save.length-1; j++){
            array[j]=save[(j+1)];
        }
        return array;
    }//end removeAndTrim

Your method is the most efficient possible assuming this is an exercise where you are not allowed to use libraries, utility classes like arraylist or System.arraycopy. 假设这是一个练习,不允许使用库,实用程序类(如arraylist或System.arraycopy),则该方法是最有效的方法。 Reasoning: 推理:

  • You can't avoid constructing a new array since a) you need one that is one element shorter and b) Java arrays are fixed size so you can't change the existing one 您不可避免地要构建一个新数组,因为a)您需要一个短一个元素的数组,并且b)Java数组是固定大小的,因此您无法更改现有数组
  • You need to copy length-1 elements in order to populate the new array. 您需要复制length-1元素才能填充新数组。 Doing this in a tight loop is the fastest you can do in pure Java. 在纯Java中,以紧密的循环进行此操作是最快的。

As a style point, you should probably call the new array "result" or somthing similar and avoid the fiddling around with trying to save the array. 作为一种风格,您可能应该将新数组称为“结果”或类似的东西,并避免随意尝试保存该数组。 This is pointless - you can't alter the input parameter. 这毫无意义-您无法更改输入参数。

Note that your function needs to be used as follows: 请注意,您的函数需要按以下方式使用:

NumberTile[] newArray=removeAndTrim(oldArray,index);
public NumberTile[] removeAndTrim(NumberTile[] a, int index){
    NumberTile[] result = new NumberTile[a.length-1];
    for (int i = 0; i < result.length; i++){
       result[i] = a[((i < index) ? i : i + 1)];
    }
    return result;
}

Your most efficient way would be one loop / traversal and one array creation. 您最有效的方法是一个循环/遍历和一个数组创建。
(Without using arraycopy that is). (即不使用arraycopy)。

Note: This doesn't alter the values of the parameter array at all, just returns a new one. 注意:这根本不会改变参数数组的值,只是返回一个新的值。

NumberTile[] removeAndTrim(NumberTile[] array, int removeIndex) {
    NumberTile[] newArray = new NumberTile[array.length - 1];
    for (int i = 0; i < removeIndex; i++) {
        newArray[i] = array[i];
    }
    for (int i = removeIndex + 1; i < array.length - 1; i++) {
        newArray[i] = array[i + 1];
    }
    return newArray;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM