2013-05-10 83 views
0

我想插入一個列表中的值,但僅當這些值不在其中。我的代碼到目前爲止創建了一個無限循環,因爲當我將值添加到列表中時,列表的大小也會增加,因此無法滿足for循環的終止條件。這是一個更大的程序的一部分,e是一種擴展Point的對象。 請注意: e擴展點。 e有一個值(除了從Point繼承的座標外)。 如果列表爲空,我會將e存儲在列表中。列表是e類型的。 如果列表不是空的,我檢查是否存在與我輸入的電子地址相同的電子地址。我不檢查e對象,而是檢查x和y值是否匹配。 更新代碼:如何檢查一個值是否已經在ArrayList中插入Java之前?

List<e> listClosestFirst = new LinkedList<e>(); 

    if (listClosestFirst.isEmpty()) { 
     listClosestFirst.add(e); 
    } 
    else { 

     for (int i = 0; i < listClosestFirst.size(); i++) { 
      if ((e.getLocation()).equals((listClosestFirst.get(i)).getLocation())) { 
       // do nothing, move on  
      } // end if 

      else { 
       listClosestFirst.add(e); 
      } 

     } // end for loop 

    } // end else statement 

System.out.println("Closest First Memory: " + listClosestFirst); 
+1

這可能有助於...... ['LinkedList#contains'](http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#contains%28java.lang.Object %29) – MadProgrammer 2013-05-10 02:01:01

+0

這是否與旅行商問題有關? – 2013-05-10 02:06:38

+0

如果你更詳細地解釋你在做什麼,我們可以幫你清理你的代碼。或者提供代碼樣本來幫助你。 – 2013-05-10 02:31:04

回答

1
boolean exist = false; 
ListIterator<e> iterator = listClosestFirst.listIterator(0); 

while (iterator.hasNext() && !exist) { 
    exist = (e.getLocation()).equals((iterator.next()).getLocation()); 
} 

if (!exist) { 
    listClosestFirst.add(e); 
} 

由於您使用鏈表,迭代器是更有效的。大概從O(n!)增加到O(n)。

+0

這非常有道理。謝謝。 – Matador89 2013-05-10 03:27:22

5

正如指出的,你可以使用的方法。然而,僅僅使用Set而不是列表可能會更好,因爲默認情況下集要求唯一性。

*代碼示例*

public void testPoints() { 
     Set<E> setClosestFirst = new LinkedHashSet<E>(); 
     for (int i = 1; i <= 100; ++i) { 
      //create 100 random points/planes 
      //add them to the set 
      E anotherRandomE = new E(Calendar.getInstance().getTime().getTime() * i); 
      setClosestFirst.add(anotherRandomE); 
     } 
     System.err.println("There were " + setClosestFirst.size() + " unique points created."); 
    } 

    public class Point { 
     protected int x; 
     protected int y; 
    } 

    /* Kind of a bad name for a class...perhaps MyCustomPoint would be better. 
     Longer names in Java are usually best. 
    */ 
    public class E extends Point { 
     private int plane; 

     public E(long seed) { 
      Random random = new Random(seed); 
      int minPlane = 0; 
      int maxPlane = 1; 
      int xYMin = 0; 
      int xYMax = 10; 
      this.plane = random.nextInt(maxPlane - minPlane) + minPlane; // random plane between 0 and 10 
      this.x = random.nextInt(xYMax - xYMin) + xYMin; 
      this.y = random.nextInt(xYMax - xYMin) + xYMin; 
     } 

     @Override 
     public boolean equals(Object o) { 
      if (this == o) return true; 
      if (!(o instanceof E)) return false; 

      E e = (E) o; 

      if (this.x != e.x || this.y != e.y || this.plane != e.plane) return false; 

      return true; 
     } 

     @Override 
     public int hashCode() { 
      return plane * x * y * 13; 
     } 
    } 
+1

+1對'Set' ... – MadProgrammer 2013-05-10 02:10:28

+2

如果你仍然想保持順序考慮一個LinkedHashSet – Elmer 2013-05-10 02:12:01

+0

它是一個更大的問題的一部分,所以我必須使用列表。 – Matador89 2013-05-10 02:28:57

相關問題