繁体   English   中英

C#-如何从通用列表中获取具有最高特定参数的项目?

[英]C# - How to get item from Generic List with the highest specific parameter?

我进行了很多搜索,但找不到足够理解的答案来翻译成我的项目。 目标是什么:我需要在列表上找到具有最高盔甲等级参数的物品,并将该物品的盔甲等级添加到角色的盔甲等级。

因此,我们以这种方式创建了一个列表:

public List<Weapon> characterInvWeapon;
public List<Armor> characterInvArmor;

等等

这是创建Armor类及其属性的方法:

public class Armor : Item, IComparable <Armor> {
  public string armor_prof;
  public string armor_category;
  public int armor_class;
  public int armor_str_req;
  public string armor_stealth_mod;

  public Armor (string c_name
      , string c_description
      , bool c_stackable
      , int c_value
      , string c_coin_type
      , int c_weight
      , string c_armor_prof
      , string c_armor_category
      , int c_armor_class
      , int c_armor_str_req   
      , string c_armor_stealth_mod) : base (c_name, c_description, c_stackable, c_value, c_coin_type, c_weight)
  {
      armor_prof = c_armor_prof;
      armor_category = c_armor_category;
      armor_class = c_armor_class;
      armor_str_req = c_armor_str_req;
      armor_stealth_mod = c_armor_stealth_mod;
  }

  public int CompareTo(Armor other)
  {
      if (armor_class == other.armor_class)
        return String.Compare (name, other.name); // a < ab < b 
      else
        return other.armor_class - armor_class;
  }
}

Armor是一个继承自Item的类,该类具有前6个属性。 装甲存储在特定于装甲的列表中-public public List<Armor> characterInvArmor;

示例项目:

AddToItemStore(new Armor("Breastplate", "Description.", false, 400, "gp", 20, "Breastplate", "Medium Armor", 14, 0, ""));

添加脚本:

public void AddToCharacterInventory(Item it)
    {
        if (it is Weapon)
        {
            charInvWeapon.Add((Weapon)it);
            charInvWeapon.Sort();
        }
        else if (it is Armor)
        {
            charInvArmor.Add((Armor)it);
            charInvArmor.Sort();
        }
    }

现在,正如我提到的,我需要在charInvArmor列表中找到具有最高armour_class参数的项目,并在其他函数中使用其值,该函数根据许多变量来计算盔甲等级。

因此在其他函数中, characterArmorClass = armorWithHighestArmorClass + otherVariable + someotherVariable; 等等

我怀疑Linq中有一些方便的快捷方式,但是如果没有Linq,我将非常感谢一些示例。 Linq也将受到欢迎,但是我对此绝对陌生,例如,我担心性能以及我的应用程序与iPhone的兼容性。 我已经阅读过iOS可能会导致Linq出现问题。 这必须是快速且兼容的计算。

使用LINQ:

int maxArmorClass = characterInvArmor.Max(armor => armor.armor_class);

如果不使用带有排序的LINQ:

var list = characterInvArmor.ToList(); // copy list, so we do not break sorted orded
list.Sort((armor1, armor2) => armor2.armor_class.CompareTo(armor1.armor_class));
int maxArmorClass = list[0].armor_class;

当然,您总是可以编写带有循环和“ max”变量的手动方法。

顺便说一句,我注意到,您在AddToCharacterInventory方法中对charInvArmor进行了排序。 如果始终对数组进行排序,则根据您的CompareTo实现,具有最大armor_class的项目应始终排在最后(或者不确定,排在第一)。 因此,只需采用列表的最后一个(第一个)元素。

您必须遍历该列表并检查每个值,因为该列表不是根据装甲等级值排序的,并且您不想使用LINQ:

int maxArmorClass = 0;
foreach (var armor in characterInvArmor)
{
    // Do a comparison here and see if you found a higher value
    // If a higher value is found, store it in maxArmorClass
}

作为旁注,我建议以下链接:

公共字段与自动属性

c#的样式指南 *在C#中,Pascal大小写和驼色大小写是已建立的约定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM