• Post Reply Bookmark Topic Watch Topic
  • New Topic

Moving through elements in ArrayList  RSS feed

 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I'm going to state this as simply as I can in hopes of getting a simply stated reply.

I need to create an ArrayList of snacks to be sold. When there is any one snack item that has only one pack left, I need to restock. When I do it item by item, I do this:



But I have to have 100 types of snack, so I figure I need an array list. I know there has to be a way to make each item in the arraylist restock without listing each item separately, as above. Can someone tell me how to do that?

Thank you very much! Class ends in just over 2 weeks!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen,
I'm not sure if I understand this properly, but you might need a Map. That way you can map snack items to quantities left.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jeanne--but I hope not! I have no idea what Java Map is, & it's all I can do to handle what I'm SUPPOSED to know!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether you use an array, an ArrayList, Map or any other collection, you can iterate through all the items without needing to code each one separately by using a loop. You are familiar with the for statement, no?
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I know that I can do this



to go through the list, but I don't know how to say in code "if any item on this list has a count of fewer than 2, make that item have 50."
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure you do. You posted it in the first topic. What you may not know, is how, within the loop, to get the current iteration item perhaps?

Are you familiar with the alternate form of the for statement? (which makes the whole thing lots easier)
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:Yes. I know that I can do this



to go through the list, but I don't know how to say in code "if any item on this list has a count of fewer than 2, make that item have 50."
That is because you don't have a class which encapsulates the type of snack and the number left. If you had such a class you could create an array of its instances and gain access to the number left easily.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!