【问题标题】:Name an array with a preexisting string使用预先存在的字符串命名数组
【发布时间】:2014-02-03 09:59:18
【问题描述】:

我有一个 for 循环,它创建一个具有设定数量索引的字符串数组并用字符串填充它。我将有第二个 for 循环来创建更多数组并将这些数组命名为我之前数组中的字符串。它不会将字符串作为名称,所以我卡住了。有什么建议可以解决这个问题?

我在 C# 中这样做。

谢谢

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class World : MonoBehaviour {

    public int WorldSize;
    const int q = 1000;
    const int w = 100;
    string [] world;

    // Use this for initialization
    void Awake () {

        world = new string[((WorldSize / q) * 2)];

        //int[,,] map = new string[((WorldSize / q) * 2)];

        for (int i = 0; i < ((WorldSize / q) * 2); i++){

            string h = i.ToString();

            world[i] = ("Region" + h);
            Debug.Log(world[i]);

        }



        Dictionary<string, string[]> regions = new Dictionary<string, string[]>();

        for (int i = 0; i<world.Length; i++) {
            string h = i.ToString();
            regions.Add(world[i], new string[q/w]);
            Debug.Log(regions["Region" + h]);
        }


    }
    }

【问题讨论】:

  • 请提供您目前拥有的代码,并让我们知道它在哪里失败/无法编译。

标签: c# arrays string set


【解决方案1】:

所以,如果我对你的理解正确,你有这样的事情:

string[] strArr = new string[...];
for...
{
    //something happens to strArr
}

现在您想为strArr 中的每个字符串元素创建一个字符串数组,并为该数组指定一个与strArr 中的值相对应的名称。如果是这种情况,您可能想看看Dictionary&lt;T, K&gt; 类:

string[] strArr = new string[...];
for...
{
    //something happens to strArr
}

Dictionary<string, string[]> myArrays = new Dictionary<string, string[]>();
foreach(string element in strArr)
{
    myArrays.Add(element, new string[size]);
}

您以后可以按如下方式访问您的数组:myArrays["index"]

一些注意事项: 上面的代码假设您事先知道数组的大小。这可能并不总是可行的,这就是为什么我建议您使用 List&lt;string&gt; 而不是字符串数组。

【讨论】:

    【解决方案2】:

    C# 是一种强类型语言,您不能创建具有动态名称的 var。 如果您需要此功能,我建议使用字典。在第一个字段中,您可以输入您需要的键,在第二个字段中,您可以输入您需要的值

    【讨论】:

      【解决方案3】:

      您的错误很可能是由错误引起的:new string[((WorldSize / q) * 2)] 创建了一个新数组,因此您不想在Awake() 的循环中执行此操作。

      试试:

      public class World : MonoBehaviour {
          public int WorldSize;
          const int q = 1000;
          const int w = 100;
          string[] world;
      
          // Use this for initialization
          void Awake () {
      
              //int[,,] map = new string[((WorldSize / q) * 2)];
              world = new string[((WorldSize / q) * 2)];
      
              for (int i = 0; i < ((WorldSize / q) * 2); i++){
                  string h = i.ToString();
                  world[i] = ("Region" + h);
                  Debug.Log(world[i]);
      
              }
              Dictionary<string, string[]> regions = new Dictionary<string, string[]>();
              foreach(string element in world)
              {
                  regions.Add(element, new string[q/w]);    
              }
              Debug.Log(regions["Region1"]);
          }
      }
      

      【讨论】:

      • 是的,我在发布代码几分钟后发现了这一点。我现在有一个新问题。数组没有名称。查看链接图片i.imgur.com/xHb16DI.png
      • @user3265317,我的公司屏蔽了你的图片,所以我看不到。
      猜你喜欢
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 2018-05-13
      • 2020-05-28
      • 2021-08-05
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多