从数组中删除特定元素

时间:2015-03-25 18:55:28

标签: java arrays

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

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表示元素值为768的3元素数组

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来制作只包含其余部分的数组!

3 个答案:

答案 0 :(得分:2)

在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()实现此目的:

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]

答案 1 :(得分:1)

您遇到的问题是无论如何都将数字存储在第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;
}

答案 2 :(得分:0)

由于你事先不知道原始数组的大小,所以稍后再转换为它。

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()]);
相关问题