Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Moving through elements in ArrayList

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author & internet detective
Posts: 41263
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Marshal
Posts: 76487
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic