簡體   English   中英

比較對象ArrayList和String ArrayList

[英]Compare Object ArrayList with String ArrayList

我有一個從數據庫中填寫的String ArraList 還有一個Object ArrayList ,每個對象都包含一些String 現在,我想將String ArrayListObject ArrayList中每個對象的String ArrayList之一進行比較。 如果等於,則必須刪除Object ArrayList的Object。 這是我的Method代碼:

        public ArrayList checkMatches(ArrayList<IceCream> iceCreams, ArrayList<String> blackListPartCodes) { // output is filtered Object ArrayList and two Input,
//one is the Object ArrayList and other is String ArrayList
        int i, j;
        for(i = 0; i<iceCreams.size(); i++) { // Object ArrayList
            IceCream iceCream = iceCreams.get(i);
            for(j = 0; j<blackListPartCodes.size(); j++) { //String ArrayList
                if ((iceCream.getPartCode().matches(blackListPartCodes.get(j)) || iceCream.getPartCode().equals(blackListPartCodes.get(j)))) {
                    iceCreams.remove(iceCream);
                }
            }
        }
            return iceCreams;
        }

好的,當我使用此方法時,它從我的對象中刪除了一些對象並減小了ArrayList的長度,但無法正常工作。 我做錯什么了嗎?

我在我的應用程序中使用了以下代碼,以查看方法是否正常運行:

            Toast.makeText(getApplicationContext(), "Before : " + iceCreams.size(), Toast.LENGTH_LONG).show(); //324

    checkMatches(iceCreams, blackListPartCodes);
    Toast.makeText(getApplicationContext(), "After : " + iceCreams.size(), Toast.LENGTH_LONG).show(); //200

iceCreams的第一lenght是324,方法交互時的lenght是200。我從DB讀取了String ArrayList(blackListPartCodes),當我使用select Count(myField) from MyTable它說它有215行。 這意味着如果它可以正常工作,應該是324-215即109。從另一面來看,我在ListView顯示每個對象的String之一。 通過這個:

        for(int i = 0; i<iceCreams.size(); i++) { // the filtered iceCreams after calling `checkMatches`method.
        typeArray.add(iceCreams.get(i).getPartName()); // String ArrayList
        typeAdapter.getItemViewType(R.id.listView); //Adapter of the Array
        iceCreamTypeList.setAdapter(typeAdapter); //Adapter set to ListView
    } 

但是在視圖中, blackListPartCodes的字段仍然存在。

我試圖簡單地模仿你的方法。 現在,它將迭代拋出您的數組,並將匹配項放入新的數組列表中。 這不會影響或更改現有陣列:

public ArrayList checkMatches(ArrayList<IceCream> iceCreams, ArrayList<String> blackListPartCodes) { 
  ArrayList<IceCream> matches = new ArrayList();

  for (IceCream iceCream : iceCreams) {
    for (String blackListPartCode : blackListPartCodes) {
      if (blackListPartCode.equals(iceCream.getPartCode()) 
          || blackListPartCode.matches(iceCream.getPartCode())) {
        matches.add(iceCream);
      }
    }
  }

  return matches;
}

如果i == 0並且找到匹配項,它將在iceCream中刪除索引為0的項目。 列表中的所有其他項目將其索引都減一。 因此,在此示例中,將不檢查最初具有索引1的項,因為其新索引為0且i == 1。

相反,請跟蹤匹配項並在遍歷列表后將其刪除。

比較相同類的對象的簡單解決方案,請按照以下步驟操作:

1. Open your IceCream.java file.

2. Add following lines of code :
       @Override

public boolean equals(Object o) {
    if (o == null) {
        return false;
    }
    if (getClass() != o.getClass()) {
        return false;
    }
    final IceCream other = (IceCream) o;
    return this.name == other.name;
}

3. Now compare your IceCream class object with another IceCream class object using equals(obj) method. 

希望! 有用。 :D

我終於找出問題所在。 我應該在內部循環中標識iceCream對象。 通過這種方式 :

        for(i = 0; i<iceCreams.size(); i++) {
        for(j = 0; j<blackListPartCodes.size(); j++) {
            IceCream iceCream = iceCreams.get(i);

不是這樣的:

    for(i = 0; i<iceCreams.size(); i++) { // Object ArrayList
        IceCream iceCream = iceCreams.get(i);
        for(j = 0; j<blackListPartCodes.size(); j++) {

暫無
暫無

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

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