• Post Reply Bookmark Topic Watch Topic
  • New Topic

Iterating through ArrayLists of Integers to find if it contains particular values  RSS feed

 
Conor Niall
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

How could I check the values of an ArrayList<Integer> to see if any of its objects, when added to another number, adds up to a certain value? For example: I want to check when added to a Player's current running total they add up to 10 - if this is true call method A, if false call method B instead.



I think I would need to iterate through each object in the Arraylist using the contains() method, but I am having difficulties understanding where, when, and how to do the iteration.

Thanks in advance
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two possibilities: One, use an enhanced for loop.  Two, use a Java 8 stream.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
makeMove() sounds like a good place. Have you learned about a for loop yet?
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Conor Niall
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:


How would I retrieve the number from the numbers.contains() statement and then be able to use this number to add/remove it from a collection?
Example:


How would I accomplish this?
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My example might be this if auto-boxing doesn't applyNot sure how auto-boxing would work in this case.

To remove
To add

 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conor Niall wrote:
How would I retrieve the number from the numbers.contains() statement and then be able to use this number to add/remove it from a collection?
Example:


How would I accomplish this?

ArrayList#remove() has a form that takes an Object as its argument.  This code:

...would remove the first "3" from numbers, if it exists.

Oops, doesn't seem to work.  Not sure why.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works (except when using Arrays.asList()).

Output
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:This works (except when using Arrays.asList())...

That's right.  Arrays.asList() creates a read-only list.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I am late to the discussion, but what about putting the contents of the List into a sorted set, then iterating the set up to n ÷ 2 where n is the desired total. For each i found, you can see whether the set contains n − i, and then look for their locations in the original List.
 
Conor Niall
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Thanks for all the suggestions. You've been very helpful as always. In the end, I went with Carey's suggestion:

This works perfectly and is the most straight-forward for me at this stage.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!