这种情况如何与 ArrayList 一起使用?

这个场景真的很不一样,我想实现,但我不知道如何去做。我的场景是这样的:


我有一个ArrayList,这ArrayList总是用新的更新。请这样想。


列表一是我的目标列表。我在这个列表中有这些项目:


乙丙


我在首次发布时两次保存此列表:list_old_one - list_new_one


几分钟后,新物品与旧物品一起出现。


A B C D


而且我正在保存这个列表: list_new_one


我想做这个。如果C不存在 to list_one_now,我只想显示这个,我想删除所有其他项目,只显示C而不是D


我厌倦了equal和removeAll部分但也即将到来的项目D因为它不相等。


我的数组列表:


TinyDB tinydb = new TinyDB(getApplicationContext());


//Adding current list to new one

tinydb.remove("list_new_one");

tinydb.putListString("list_new_one", all_list);


ArrayList<String> list_old = new ArrayList<>(tinydb.getListString("list_old_one"));

ArrayList<String> list_new = new ArrayList<>(tinydb.getListString("list_new_one"));


   list_old.removeAll(list_new);

   if (list_new.size() != 0) {

        //Adding results to database

        DBRecentlyHelper dbRecentlyHelper = new DBRecentlyHelper(getApplicationContext());

        dbRecentlyHelper.RecentlyADD(list_old, current_user);

        dbRecentlyHelper.close();


       //Updating old list

       tinydb.remove("list_old_one");

       tinydb.putListString("list_old_one", all_list);

  }else{

       //Updating old list

       tinydb.remove("list_old_one");

       tinydb.putListString("list_old_one", all_list);

  }

我真的在找一个星期,但仍然得到错误的结果。


如果有人有想法,我会很高兴。非常感谢。


慕的地6264312
浏览 134回答 3
3回答

海绵宝宝撒

你可以试试这个:myList.stream().filter(item&nbsp;->&nbsp;item.equals("C")).collect(Collectors.toList());由于 list_new 中不存在“D”项,因此 removeAll 无法将其删除。如果您需要检查您的列表是否包含“C”项,您可以这样做:if(myList.contains("C")){myList&nbsp;=&nbsp;myList.stream().filter(item&nbsp;->&nbsp;item.equals("C")).collect(Collectors.toList());}您不需要使用两个列表来删除所有其他项目。

忽然笑

成功的答案是好的方法,也是困难的方法。因为你不需要得到物品。Android 操作系统总是可以等同于短文本或长文本。代替:list_old.removeAll(list_new);到:for(String i: new ArrayList<>(list_old)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(String j: new ArrayList<>(list_new)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!i.equals(j)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list_old.add(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }你会得到正确的结果。

LEATH

您得到错误的列表,因为您将列表等同于列表。这样做;创建一个像 SQLite 一样的 SQLite DBRecentlyHelper。例如:DBOldListItems和DBOldListItems将所有项目保存到我所说的新数据库中,并将其与项目相等。旧物品:A - B - C新东西:A - B - C - D例子:if(!olditems.get(i).item.equals(newitems.get(i)){&nbsp; &nbsp; olditems.get(i).item}如果, A item 将不等于 newitems 数组,您将得到结果。在结果之后,您可以更新您的旧项目,并且您可以执行更多、更多、更多的操作。如果您有什么不明白的地方,请与我联系。也许我的英语听不懂:)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java