Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Java]How to check if one ArrayList contains objects stores in another

  1. Aug 31, 2014 #1
    I have two ArrayLists and I want to check if the items stored in one list is also in the other and if it is, store it in a third ArrayList. My problem is that each object from list two is getting stored in list three.

    The lists contain:

    List one: 50 items
    List two: 15 items

    Of the 15 items in list two, 10 of them are in List one and these are the ten that I want to store in List three.

    Here is the code I have came up with but something is not right. I didn't see it necessary to show the rest of the code since I know storing the objects in list one and two is working properly.

    view plainprint?
    Note: Text content in the code blocks is automatically word-wrapped
    Java:
    for (Items item : listTwo){
    listThree.add(listOne.contains(item) ? 1 : 0, item);

    }



    ¸.•°*”˜˜”*°•. ¸.•°*”˜˜”*°•.¸
    ..¸.•°*”˜˜”*°•Thank you.•°*”˜˜”*°•.¸

    ————————————————
     
    Last edited: Aug 31, 2014
  2. jcsd
  3. Aug 31, 2014 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    You are misunderstanding what the "," operand does. The expression "listOne.contains(item) ? 1 : 0, item" first evaluates "listOne.contains(item) ? 1 : 0" giving either 1 or 0, then throws away that result, then evaluates "item", which is just a reference to "item". So apart from wasting a bit of time checking in the item is in listOne, your code does the same thing as "listThree.add(item)", which of course always adds "item" to listThree.

    If you really want to use the "? :" operator, you could write something like
    Code (Text):

    listOne.contains(item) ? listThree.add(item) : 0;
     
    but most people would just write
    Code (Text):

    if (listOne.contains(item))
       listThree.add(item);
     
     
  4. Sep 2, 2014 #3
    Thank you
     
  5. Sep 2, 2014 #4

    Filip Larsen

    User Avatar
    Gold Member

    Java does not have a comma operator like C. Instead, what happens is that the add(int,E) method is invoked, that is, the first argument in the invocation will be either 0 or 1 depending on whether the item from the second list exist in the first list or not. Inserting in position 0 or 1 means the item will be insert first or second respectively in the list. If the first item in the second list is contained in the first list, then add is invoked with index 1 on an empty list which would give an IndexOutOfBounds exception.
     
  6. Sep 15, 2014 #5

    harborsparrow

    User Avatar
    Gold Member

    If the "items" stored in your ArrayLists are objects, you can use .Equals to test for "equality", which really checks the memory address.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Java]How to check if one ArrayList contains objects stores in another
  1. Check java version (Replies: 4)

Loading...