2017-10-10 32 views
0

我試圖刪除那些基於它們在值列表中的位置的特定項目。 下面的代碼,如複製列表縮小太多如何根據列表中的位置從列表中刪除值?

private ArrayList<String> removeGen(int addLines, int labLines, int venLines, ArrayList<String> values){ 
     ArrayList<String> copy = new ArrayList<>(); 
     copy = (ArrayList<String>) values.clone(); 
     for(int i = 0; i < values.size(); i++) { 
      if (i > 100 && i < (100 + (addLines * 5) + 1)) { 
       copy.remove(i); 
      } 

      if (i > (100 + (addLines * 5)) + 9 && i < 100 + (addLines * 5) + (labLines * 4) + 10) { 
       copy.remove(i); 
      } 

      if (i > 100 + (addLines * 5) + (labLines * 4) + 21 && i < 100 + (addLines * 5) + (labLines * 4) + (venLines * 4) + 22) { 
       copy.remove(i); 
      } 
     } 

     return copy; 

的問題是一些值從一個區分彼此,所以我也不能使用卸妝(OBJ)拋出一個錯誤。

我該如何返回一個列表,並在上述位置刪除值?

+0

你應該能夠刪除這樣的條目,因爲你是通過索引而不是按值來刪除某些內容的。 – LenglBoy

+0

仍然複製列表的大小減少,因爲它的項目被刪除製作,他們的位置變化都很好雖然檢查了這一點 – Jube

回答

1

您正在刪除索引而不是數值,但是您忘記了values的索引是除去某物後copy中的索引。 每個對象在刪除後都會得到另一個索引,因此列表不再一樣。

示例: 刪除索引4後,索引5正在索引4上獲取新值。此切換效果將一直持續到列表結束。

因此,有兩種方法可以使這一個正確的:

  1. 轉身的條件和所有有效數據添加到copy,並給這個名單了。
  2. 將指針i處的值清空,然後在返回之前過濾列表。 return copy.filter(x -> x != null).collect(Collectors.toList()); 如果使用的Java 7 you're,降低您可以使用copy.removeAll(Collections.singleton(null));

這裏的第一個結果:

ArrayList<String> copy = new ArrayList<>(); 
for(int i = 0; i < values.size(); i++) { 
    if (i < 100 && i > (100 + (addLines * 5) + 1)) { 
     copy.add(values.get(i)); 
    } 

    if (i < (100 + (addLines * 5)) + 9 && i > 100 + (addLines * 5) + (labLines * 4) + 10) { 
     copy.add(values.get(i)); 
    } 

    if (i < 100 + (addLines * 5) + (labLines * 4) + 21 && i > 100 + (addLines * 5) + (labLines * 4) + (venLines * 4) + 22) { 
     copy.add(values.get(i)); 
    } 
} 

return copy; 

在這裏,第二個結果:

ArrayList<String> copy = new ArrayList<>(); 
copy = (ArrayList<String>) values.clone(); 
for(int i = 0; i < values.size(); i++) { 
    if (i > 100 && i < (100 + (addLines * 5) + 1)) { 
     copy.get(i) = null; 
    } 

    if (i > (100 + (addLines * 5)) + 9 && i < 100 + (addLines * 5) + (labLines * 4) + 10) { 
     copy.get(i) = null; 
    } 

    if (i > 100 + (addLines * 5) + (labLines * 4) + 21 && i < 100 + (addLines * 5) + (labLines * 4) + (venLines * 4) + 22) { 
     copy.get(i) = null; 
    } 
} 

return copy.filter(x -> x != null).collect(Collectors.toList()); 
0
private ArrayList<Integer> removeGen(int addLines, int labLines, int venLines, ArrayList<String> values){ 
     ArrayList<String> copy = new ArrayList<>(); 
     ArrayList<Integer> numbers = new ArrayList<>(); 
     copy = (ArrayList<String>) values.clone(); 
     for(int i = 0; i < values.size(); i++) { 
      if (i > 100 && i < (100 + (addLines * 5) + 1)) { 
       //copy.remove(i); 
      } 
      else if(i > (100 + (addLines * 5)) + 9 && i < 100 + (addLines * 5) + (labLines * 4) + 10) { 
       //copy.remove(i); 
      } 

      else if(i > 100 + (addLines * 5) + (labLines * 4) + 21 && i < 100 + (addLines * 5) + (labLines * 4) + (venLines * 4) + 22) { 
       //copy.remove(i); 
      } 
      else { 
       numbers.add(i); 
      } 


     } 
     return numbers; 

    } 

只是做相反,並提取我需要的位置,應該有助於他人

ArrayList<Integer> tempList = removeGen(additional,labour,vendor,values); 

     for (int j = 0; j < tempList.size(); j++) { 
      template = template.replaceFirst(section + j, values.get(tempList.get(j))); 
     }