Updating an array list with new items only

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.

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

2 Likes

Adding to your answer, you can also use indexOf():

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) 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

1 Like

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