簡體   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