【问题标题】:How to match 2 lists with the same index position?如何匹配具有相同索引位置的 2 个列表?
【发布时间】:2021-04-27 16:48:25
【问题描述】:

使用 c# 嗨,我需要匹配 2 个列表,从 1 个列表中获取 1 个 valor,到同一范围内的另一个列表。

是这样的:

public void SetText(TextMeshProUGUI texto )
{
    string mensaje;
    
        foreach (Collider2D x in collidersTextoEmergente)
    {
        nombreColider.Add(x.name);

        for (int i = 0; i < nombreColider.Count; i++)
        {
            for (int b = 0; i < msgEmergentes.Count; i++)
                  mensaje = msgEmergentes[b];

            switch (nombreColider[i])
            {
                case "basura":
                    texto.text = mensaje;
                    break;
                case "DinnerTime":
                    texto.text = mensaje;
                    break;

                case "atomo":
                    texto.text = mensaje;
                    break;
            }
        }
    }
}

真是一团糟…… 我希望我的函数协调触发器或对撞机名称并自动设置文本,即我已经在 msg 列表中设置的文本。 现在我用了这个但是是手动的....

 private void OnTriggerEnter2D(Collider2D collision)
{

    SetText(collision);
}

public void SetText(Collider2D other)
{
    switch (other.name)
    {
        case "basura":
            cosita.text = msgEmergentes[0];
            break;
        case "DinnerTime":
            cosita.text = msgEmergentes[1];
            break;

        case "atomo":
            cosita.text = msgEmergentes[2];
            break;
        case "extintor":
            cosita.text = msgEmergentes[3];
            break;
    }

}

【问题讨论】:

  • 为了澄清您的问题,请给我们您期望的输出和实际得到的输出。
  • 已经完成了,但是谢谢
  • 如果你想通了,请回答你自己的问题。没有什么比找到其他人遇到类似问题并解决它但从未分享他们的解决方案更让开发人员烦恼的了。见https://xkcd.com/979/
  • 如何分享我的问题?

标签: c# list unity3d indexing match


【解决方案1】:

在我看来,您可以使用Dictionary 以简化的方式实现这一目标

选项 A

宁可使用例如像

这样的字典
private Dictionary<string, int> indexByName = new Dictionary<string, int>()
{
    {"basura", 0},
    {"DinnerTime", 1},
    {"atomo", 2},
    {"extintor", 3},
}

private void OnTriggerEnter2D(Collider2D collision)
{
    SetText(collision);
}

public void SetText(Collider2D other)
{
    if(indexByName.TryGetValue(other.name, out var index)
    {
        cosita.text = msgEmergentes[index];
    }
}

选项 B

为什么还要通过索引进行路由?直接用字典就好了

private Dictionary<string, string> msgByName = new Dictionary<string, string>()
{
    {"basura", "Whatever basura message"},
    {"DinnerTime", "Hey, it's dinner time!"},
    {"atomo", "Whatever other message"},
    {"extintor", "Yet another message!"}
}

private void OnTriggerEnter2D(Collider2D collision)
{
    SetText(collision);
}

public void SetText(Collider2D other)
{
    if(msgByName.TryGetValue(other.name, out var msg)
    {
        cosita.text = msg;
    }
}

选项 C

现在,如果您仍然希望能够在 Unity Inspector 中调整这些以更灵活,您可以执行类似的操作

[Serializable]
public class KeyMessagePair
{
    public string name;
    public string message;
}

// Adjust these in the Inspector
public KeyMessagePair[] pairSetup;

private Dictionary<string, string> msgByName;

private void Awake ()
{
    msgByName = new Dictionary<string, string>();
    foreach(var pair in pairSetup)
    {
        msgByName.Add(pair.name, pair.message);
    }
}

private void OnTriggerEnter2D(Collider2D collision)
{
    SetText(collision);
}

public void SetText(Collider2D other)
{
    if(msgByName.TryGetValue(other.name, out var msg)
    {
        cosita.text = msg;
    }
}

当然,您可以/应该添加一些机制以避免重复名称,但我认为现在应该这样做;)

【讨论】:

  • 这看起来太好了...从来没有用过字典所以让我看看它是如何工作的,我试一试
  • 是的,这很有效
  • @AgustinGimenezRojas 刚刚添加了一个版本,您仍然可以通过检查器而不是硬编码字典进行调整;)
【解决方案2】:

您似乎正在尝试将一个列表复制到另一个列表中?如果第二个列表是空的,你可以这样做:

for(int i = 0; i < firstList.Count; i++)
{
    secondList[i] = firstList[i];
}

请记住,根据列表中的值,这将生成浅拷贝,而不是深拷贝。更多信息here.

【讨论】:

  • 这只是将 chatmsg[0] 中的字符串 valor 分配给同一范围内另一个列表中的人。 chatmsg[1] 等于 person[1] 等等。所以,我只需要放一个需要的字符串文本
  • @AgustinGimenezRojas 所以?
  • 我可以使用 swith 来做到这一点,并将这个字符串文本分配给每个“case”,但我看起来是自动的,只需将 1 项(人)添加到列表中,该方法就知道他的位置和 assing一个字符串 valor 但手动评估 valor 可能更复杂....
  • @AgustinGimenezRojas nono,忘记开关。尝试添加项目、人员等的代码来改进您的问题,我会更新我的答案^^
  • 事实上...我必须承认,我是编程和使用列表的新手,所以我正在尝试但我什么也没得到。但我会更新我的答案,看看我不起作用的实际代码:P
猜你喜欢
  • 2014-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
相关资源
最近更新 更多