# Updating an array list with new items only

**URL:** <https://discourse.processing.org/t/updating-an-array-list-with-new-items-only/4047>\
**Category:** Processing for Android\
**Created:** [October 2, 2018, 2:16pm UTC](https://discourse.processing.org/t/updating-an-array-list-with-new-items-only/4047 "2018-10-02T14:16:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://discourse.processing.org/u/gonadgranny)\
**Post date:** [October 2, 2018, 2:16pm UTC](https://discourse.processing.org/t/updating-an-array-list-with-new-items-only/4047/1 "2018-10-02T14:16:17Z")

</div>

could anyone please advise me on the best way to update an array list by adding only the new items(items which are not on the list already) which appear on another list? the problem i am having is that the order in which the items appear in this new list changes every time it is updated (they are bluetooth devices) and so its making the process rather difficult. thanks.

---

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://discourse.processing.org/u/gonadgranny)\
**Post date:** [October 2, 2018, 2:34pm UTC](https://discourse.processing.org/t/updating-an-array-list-with-new-items-only/4047/2 "2018-10-02T14:34:23Z")

</div>

Scratch that, ive figured it out eventually. i used the arrayList.contains() method to check whether there were any new elements. thanks.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [October 4, 2018, 5:12am UTC](https://discourse.processing.org/t/updating-an-array-list-with-new-items-only/4047/3 "2018-10-04T05:12:26Z")

</div>

Adding to your answer, you can also use [indexOf()](https://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html#indexOf(java.lang.Object)):

> Returns: the index of the first occurrence of the argument in this list; returns -1 if the object is not found.

So if it returns negative, it is not in the list. Alternatively, you can also use sets (for instance, check [here](http://www.java2s.com/Tutorial/Java/0140__Collections/SetandHashSet.htm)) which is a data structure that adds to the list only if the item is not currently in the list. You can find more about these structures in generic java documentation.

Kf

---

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://discourse.processing.org/u/gonadgranny)\
**Post date:** [October 4, 2018, 9:55am UTC](https://discourse.processing.org/t/updating-an-array-list-with-new-items-only/4047/4 "2018-10-04T09:55:16Z")

</div>

Great I’ll will have a look at both of these, thanks!
