繁体   English   中英

从数组中删除特定元素

[英]Deleting specific elements from an array

完成一种公共静态方法的实现

greaterThan()

返回类型: int[]

参数列表:一个int[] array参数列表,和一个int参数v

行动:返回一个新数组,其元素值是int[]数组参数列表中大于int参数v那些值。 考虑以下代码段

int[] array = { 7, -1, -4, 2, 1, 6, 1, -3, 2, 0, 2, -7, 2, 8 };

int[] g1 = Determine.greaterThan( array, 2 );

int[] g2 = Determine.greaterThan( array, 7 );

int[] g3 = Determine.greaterThan( array, 9 );

导致数组变量

g1表示一个3元素数组元素值76 ,和8

g2 ,并表示元素值为8的1元素数组

g3表示一个0元素数组

这是我到目前为止的内容:

public class Determine {

// method greaterThan(): reutrns new int[] array whose element values are the ones
// in list greater than v 
public static int[] greaterThan( int[] list, int v){

    int n = list.length;
    int[] x = new int[ n ];

    for( int i = 0; i < n; i++ ){

        int value = list[i];
        if( value > v ){

            x[i] = value;
        }

    }

    return x;
  }  

}

但这给了我以下结果:

greaterThan( [ 7 -1 -4 2 1 6 1 -3 2 0 2 -7 2 8 ], 2 ): [ 7 0 0 0 0 6 0 0 0 0 0 0 0 8 ]

greaterThan( [ 7 -1 -4 2 1 6 1 -3 2 0 2 -7 2 8 ], 7 ): [ 0 0 0 0 0 0 0 0 0 0 0 0 0 8 ]

greaterThan( [ 7 -1 -4 2 1 6 1 -3 2 0 2 -7 2 8 ], 9 ): [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]

因此,我基本上需要删除0以使该数组仅包含其余部分!

在Java 8中,可以使用filter()轻松完成此操作:

static int[] greaterThan(int[] list, int v) {
    return Arrays.stream(list).filter(e -> e > v).toArray();
}

通过将list转换为流,然后过滤大于v的元素,再次将流转换为数组并返回它,可以工作。

如果您不能使用Java 8,或者不允许使用流,则可以使用Arrays.copyOf()实现此Arrays.copyOf()

static int[] greaterThan(int[] list, int v) {
    // Create an array with the same length as list
    int[] greaterThanV = new int[list.length];

    // Index to be used in by the greaterThanV array
    int numGreater = 0;
    for (int i = 0; i < list.length; i++) {
        int value = list[i];
        if (value > v) {
            // Store the value and increment the numGreater index
            greaterThanV[numGreater++] = value;
        }
    }
    // Return an array containing the first numGreater elements of greaterThanV
    return Arrays.copyOf(greaterThanV, numGreater);
}

与此方法的不同之处在于,它使用numGreater作为结果数组的索引( greaterThanV ),并且仅在存储元素时对其进行递增。 这意味着,如果您的调用等效于greaterThan([7 -1 -4 2 1 6 1 -3 2 0 2 -7 2 8], 2) ,而不是返回:

[7 0 0 0 0 6 0 0 0 0 0 0 0 8]

greaterThanV数组将包含:

[7 6 8 0 0 0 0 0 0 0 0 0 0 0]

最后,由于我们存储了三个值,因此numGreater将为3。因此,当我们这样做时:

Arrays.copyOf([7 6 8 0 0 0 0 0 0 0 0 0 0 0], 3)

我们得到修剪后的数组作为结果:

[7 6 8]

您遇到的问题是无论如何都将数字存储在第i个位置。 当输入数字更大时,它将移动到数组中的下一个索引,这意味着该位置将存储一个0。

我要解决此问题的方法是创建一个仅在将数字添加到输出数组后才递增的计数器。

public static int[] greaterThan( int[] list, int v){

    int n = list.length;
    int[] x = new int[ n ];
    int counter = 0;                // added this

    for( int i = 0; i < n; i++ ){

        int value = list[i];
        if( value > v ){

            x[counter] = value;   // changed this
            counter++;            // make sure to increase the counter!

        }

    }

    return x;
}

由于您事先不了解原始数组的大小,因此请稍后转换为原始数组。

List<Integer> newList = new ArrayList<Integer>();
//Code to check and store the elements
for(int i = 0; i<list.length; i++) {
    if(list[i] > n)
      newList.add(list[i]);
}
return newList.toArray(new int[list.size()]);

暂无
暂无

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

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