簡體   English   中英

如何刪除值相等的數組元素?

[英]How to remove the elements of an array which are equal in value?

在java代碼中使用ArrayUtills可以從java數組中刪除元素。 下面是刪除特定索引處元素的代碼(代碼中為'2',刪除元素值等於'10'。

import java.util.Arrays;

import org.apache.commons.lang3.ArrayUtils;


public class RemoveObjectFromArray{

 public static void main(String args[]) {


     int[] test = new int[] { 10, 13, 10, 10, 105};

     System.out.println("Original Array : size : " + test.length );
     System.out.println("Contents : " + Arrays.toString(test));

     //let's remove or delete an element from Array using Apache Commons ArrayUtils
     test = ArrayUtils.remove(test, 2); //removing element at index 2

     //Size of array must be 1 less than original array after deleting an element
     System.out.println("Size of array after removing an element  : " + test.length);
     System.out.println("Content of Array after removing an object : "
                       + Arrays.toString(test));

 } }

它將輸出提供為:

run:
Original Array : size : 5
Contents : [10, 13, 10, 10, 105]
Size of array after removing an element  : 4
Content of Array after removing an object : [10, 13, 10, 105]

如何修改代碼以獲得以下輸出:

run:
Original Array : size : 5
Contents : [10, 13, 10, 10, 105]
Size of array after removing an element  : 2
Content of Array after removing an object : [ 13, 105]

試試這個代碼

while (ArrayUtils.contains(test, 10))  {
  //let's remove or delete an element from Array using Apache Commons ArrayUtils
  test = ArrayUtils.removeElement(test, 10); //removing element with value 10
}

它應該解決你的問題。

我會在你的情況下使用ArrayList,但如果你想堅持使用你的數組,你可以執行以下操作:

int count = 0;
for(int i = 0; i < test.length; i++){
  if (test[i] == 10) {
    count++;
  }
 }
 int[] newTest = new int[count];
 count = 0;
 for( int = 0; i < test.length; i++){
   if(test[i] != 10){
     newTest[count++] = test[i];
   }
  }

我沒有測試它

雖然不是一種優雅的方式,但如果你只想使用數組,你可以使用以下內容:

public static void removeDupes(int[] array) {

    int[] forbiddenNumbers = new int[array.length];

    Arrays.sort(array);

    for(int number : array) {
        if(ArrayUtils.contains(forbiddenNumbers, number){
            int index = Arrays.binarySearch(array, number);
            while(index >= 0) {
                ArrayUtils.remove(array, index);
                index = Arrays.binarySearch(array, number);
            }
            ArrayUtils.remove(forbiddenNumbers, Arrays.binarySearch(forbiddenNumbers, number);
        } else {
            ArrayUtils.add(forbiddenNumbers, number);
        }
    }
}

這應該適用於陣列中存在的任何欺騙。

這樣做基本上是通過數組,保存另一個數組中的所有唯一數字,一旦在數組中遇到重復,它將刪除原始數組中該數字的所有實例,之后它從唯一數字數組中刪除數字因為它不再存在於原始數組中。

這是一個非常難以破解的解決方案,您應該認真考慮使用其他方法(例如使用集合),但如果您想在純數組中進行,那么這應該可行。

這只能在一個循環中完成,而不會增加來自其他庫或數據結構的開銷,復雜度為O(n)

        int[] test = new int[] { 10, 13, 10, 10, 105};
        int tobeDel = 10;
        int[] tmp = new int[test.length];
        int j=0;
        for (int i=0;i<test.length;i++) {
            if (test[i] == tobeDel) {
                continue;
            }    
            tmp[j] = test[i];
            j++;
        }
        int[] result = new int[j];
        System.arraycopy(tmp, 0, result, 0, j);

如果您編寫自己的方法,則返回result

解決方案1

我從來沒有為您的要求找到任何單一功能,但這段代碼可以做到。

while(ArrayUtils.removeElement(test,10).length!=test.length)
    test=ArrayUtils.removeElement(test,10);

您可以使用removeElement ,它將刪除元素並返回新數組。 如果沒有要刪除的內容,則會返回相同的列表。


解決方案2

這也將做同樣但沒有任何迭代。 如果這就是你要找的東西。

Arrays.sort(test);
int[] finalValue =ArrayUtils.subarray(test, 0, ArrayUtils.indexOf(test,10));
ArrayUtils.addAll(finalValue,ArrayUtils.subarray(test,ArrayUtils.lastIndexOf(test,10), test.length));

這將對數組進行排序,並將數組從開始到第一次出現,然后從最后一次出現到數組結束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM