2014-05-24 143 views
1

我有一個整數[]的集合,我想從中刪除一些元素,但似乎通常的刪除方法不起作用。 除了迭代和比較Integer []的每個元素之外是否還有其他解決方法。 下面是這個問題的測試代碼。如何從集合中刪除數組?

import java.util.ArrayList; 
import java.util.List; 


public class Test { 

    public static void main(String args[]){ 
     List<Integer[]> subSet = new ArrayList<Integer[]>(); 
     subSet.add(new Integer[]{0,1,2,3,4}); 
     subSet.add(new Integer[]{0,1,2,3,5}); 
     subSet.add(new Integer[]{0,0,0,0,0}); 
     //prune this collection by removing an Integer[] 
     Integer[] elementToRemove = {0,0,0,0,0}; 
     System.out.println("Before " + subSet.size()); 
     subSet.remove(elementToRemove); 
     System.out.println("After " + subSet.size()); 
     // 
    } 
} 
+5

數組不會覆蓋等於所以你真的不能,除非你保留一個參考t o添加的陣列。 – assylias

+0

你知道如何迭代嗎? – icbytes

回答

5

這絕對有可能與iterator。由於陣列不會覆蓋equals,因此您需要靜態的Arrays方法。

public static void main(String args[]){ 
     List<Integer[]> subSet = new ArrayList<Integer[]>(); 
     subSet.add(new Integer[]{0,1,2,3,4}); 
     subSet.add(new Integer[]{0,1,2,3,5}); 
     subSet.add(new Integer[]{0,0,0,0,0}); 
     //prune this collection by removing an Integer[] 
     Integer[] elementToRemove = {0,0,0,0,0}; 
     System.out.println("Before " + subSet.size()); 

     //get your Integer[] iterator 
     Iterator<Integer[]> it = subSet.iterator(); 
     //for each element in the array 
     while(it.hasNext()){ 

      //check if it is equal to the elementToRemove 
      if(Arrays.equals(it.next(), elementToRemove)){ 
       //remove it 
       it.remove(); 
      }     
     } 
     System.out.println("After " + subSet.size()); 
    } 
1

嘗試這種解決方案,它工作正常:

IntegerArray類:

public class IntegerArray { 

    public final Integer[] array; 

    public IntegerArray(Integer... array) { 
     this.array = array; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (o instanceof IntegerArray) { 
      return Arrays.equals(this.array, ((IntegerArray) o).array); 
     } 
     return false; 
    } 

} 

測試類:

public class Test { 

    public static void main(String args[]){ 
     List<IntegerArray> subSet = new ArrayList<IntegerArray>(); 
     subSet.add(new IntegerArray(0,1,2,3,4)); 
     subSet.add(new IntegerArray(0,1,2,3,5)); 
     subSet.add(new IntegerArray(0,0,0,0,0)); 
     //prune this collection by removing an Integer[] 
     IntegerArray elementToRemove = new IntegerArray(0,0,0,0,0); 
     System.out.println("Before " + subSet.size()); 
     subSet.remove(elementToRemove); 
     System.out.println("After " + subSet.size()); 
    } 
} 
+1

public int hashCode(){ \t \t final int prime = 31; \t \t int result = 1; \t \t result = prime * result + Arrays.hashCode(array); \t \t返回結果; \t} \t @覆蓋 \t公共布爾等於(obj對象){ \t \t如果(這種== OBJ) \t \t \t迴歸真實; \t \t if(obj == null) \t \t \t return false; \t \t如果(的getClass()= obj.getClass()!) \t \t \t返回FALSE; \t \t IntegerArray other =(IntegerArray)obj; \t \t if(!Arrays.equals(array,other.array)) \t \t \t return false; \t \t return true; \t} – Shakti

+0

是的,你是對的。 –