I am trying to use a foreach to select a Creature from an existing player, the Creature exists in the Creature Vector under m_creature however I can’t workout the format of the foreach in Java.
I have written the code as I would write it in C# and I was hoping someone could point out the differences that I should apply to have this working in my Java application. I have been using Vectors instead of lists.
public List<Creature> SelectCreature(String Name)
{
List<Creature> foundCreature = new List<Creature>();
//For the customer name that equals what has been searched...
foreach (Creature c in m_creature)
{
//
if (c.CreatureName.Equals(Name, StringComparison.OrdinalIgnoreCase))
foundCreature.Add(c);
}
return foundCreature;
}
The
foreachcommand in java uses the same oldforkeyword:It would serve you well to consult the Java API, as well as to use an IDE with code completion and object property listing, such as Eclipse.
Also, unlike C#, notice that common practice in Java is to have object methods be lower camel case, so the list method is
add, and the comparison method isequals, as pointed out in the comments.Useful links:
String API
for statement
List API