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

  • Context: Java 
  • Thread starter Thread starter sinhasiha
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around checking if objects in one ArrayList are contained within another ArrayList in Java, and how to correctly implement this functionality to store matching items in a third ArrayList. The focus is on code implementation and understanding Java's syntax and behavior.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their intention to check for common items between two ArrayLists and store those in a third ArrayList, providing an initial code snippet.
  • Another participant points out a misunderstanding regarding the use of the comma operator in the provided code, explaining that the current implementation does not achieve the intended functionality.
  • A suggestion is made to use a conditional statement instead of the ternary operator for clarity and correctness in adding items to the third list.
  • Further clarification is provided about how Java's add method works, particularly regarding the potential for an IndexOutOfBounds exception when using the add(int, E) method incorrectly.
  • A participant notes that if the items are objects, the .equals method should be used to check for equality, which involves comparing memory addresses.

Areas of Agreement / Disagreement

Participants express differing views on the correct implementation of the code, with some clarifying misunderstandings and others suggesting alternative approaches. No consensus is reached on a single correct method for achieving the desired functionality.

Contextual Notes

There are limitations in the initial code regarding the handling of indices and the use of the comma operator, which may lead to exceptions or unintended behavior. The discussion does not resolve these issues fully.

sinhasiha
Messages
2
Reaction score
0
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:
Technology news on Phys.org
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:
listOne.contains(item) ? listThree.add(item) : 0;
but most people would just write
Code:
if (listOne.contains(item)) 
   listThree.add(item);
 
Thank you
 
AlephZero said:
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".

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.
 
If the "items" stored in your ArrayLists are objects, you can use .Equals to test for "equality", which really checks the memory address.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
3K
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K