Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7657359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:00:44+00:00 2026-05-31T13:00:44+00:00

It must be really basic but I need help. For example, you store monster

  • 0

It must be really basic but I need help. For example, you store monster information in an array, and do for~loop to make each monster attacks/moves in their turn like this

for( i <- 0 to monsters.length-1) monsters(i).act 

Then some monsters die during the loop and you have to delete some elements in the array while the loop is still on going. Then the next item in the array could be not really the the next one you want to process.

is there any fast/smart way to make sure each item in an array will be proceed once and only once within the loop, even if you really had to make change to the array during loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:00:46+00:00Added an answer on May 31, 2026 at 1:00 pm

    Scala’s collections generally don’t assume that you’ll be manipulating them while they’re using a method like foreach (or executing a for loop). If you want to do things that way, the easiest class to use is Java’s java.util.concurrent.ConcurrentSkipListMap.

    // This helps you use Java collections like Scala ones
    import collection.JavaConversions._
    
    case class Monster(name: String, hp: Int) {}
    val horde = new java.util.concurrent.ConcurrentSkipListMap[Int,Monster]
    
    horde put (0, Monster("wolf",7))
    horde put (1, Monster("orc",3))
    
    for (h <- horde) println(h)   // Prints out both
    
    Iterator.iterate(Option(horde.firstEntry)) {
      case None => None
      case Some(e) =>
        val m = e.getValue
        if (m.name=="wolf") horde.remove(1)     // Kill the orc
        else if (m.name=="orc") horde.remove(0) // Kill the wolf
        Option(horde.higherEntry(e.getKey))
    }.takeWhile(_.isDefined).foreach(_=>())
    
    for (h <- horde) println(h)   // Prints out just the wolf
    

    Now, granted, this is rather a mess, but it does work, and it gives nice random access to your monsters. You have to maintain the keys in a sensible order, but that’s not too hard.

    Alternatively, as others have indicated, you could add an isAlive or isDead method, and only act on monsters that are alive. Then, after you’ve passed through the list once, you .filter(_.isAlive) to throw away all the dead monsters (or .filter(! _.isDead)), and run it again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This must be really obvious, but how do I make a winsock listen to
This question must be really basic, but if you have any pointers I would
I must really be missing something obvious, but I'm having trouble with general use
I know there must be a really simple answer to this question, but I
I know SQL well but I must be missing something really dumb here. This
I need to create a really basic flash video (actionscript 2) where I click
Really basic (and probably quite idiotic) question but I'm not familiar with this procedure
My .NET application (VB.NET 3.5 if you really must know) forms do not properly
This has been annoying me for a while yet the solution must be really
I must be doing something wrong here (because really, what are the chances of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.