【问题标题】:Can anyone explain with multidimensional arrays in C#谁能用 C# 中的多维数组解释
【发布时间】:2019-03-02 08:37:38
【问题描述】:

我是新手,很困惑。如何设置 arr[55000][7],将值保存在服务器中(使用带有 C# 的 ASP.NET)并在需要时增加数组范围。我没有找到在 C# 中更改二维数组大小的方法。

另外,我的要求是:

Variable Name      Values 
Variable 1           =  ( 2, 4) , ( 5, 1)           
Variable 2            =   ( 7, 4) , ( 1, 3)           
Variable 3            =   ( 5, 7) , ( 5, 1)           
Variable 4            =   ( 2, 4) , ( 2, 1)             
Variable 5            =   ( 9, 8) , ( 2, 4)           
Variable 6            =   ( 3, 4) , ( 5, 7)           
Variable 7            =   ( 4, 7) , ( 4, 2)           
Variable 8           =   ( 5, 4) , ( 5, 7)
Etc….    
(This data will be stored on server.) 

Now for example user give input variable 1, 4, 5, 7
The calculator will show result =( 2, 4).

我正在着手解决这个问题,但没有任何帮助。有人可以指导我吗

【问题讨论】:

  • 数组不能在 c# 中改变大小,改用List<T>
  • 从 .NET 2.0 开始,Array.Resize() 可用于重新分配数组。 Array.Resize() 创建一个新数组,将旧数组的内容复制到新数组中,并将传递的数组引用替换为对新数组的引用。
  • 谢谢 Nils 和 schgab,如果你能给我一个关于这件事的例子,我真的很感激。
  • 使用 List ,你将能够动态添加或删除元素,你会为自己省去很多痛苦。
  • with List 我将无法使用正确的变量名访问元素,如果可以做到,我会调查它。请确认

标签: c# arrays multidimensional-array


【解决方案1】:

这将是一个列表列表的简单实现:

       List<List<int>> myList = new List<List<int>>();

       myList.Add(new List<int>());
       myList.Add(new List<int>());
       myList.Add(new List<int>());


       int list = 0;
       int element = 6;

       myList[0].Add(5);
       myList[list].Add(element);

如果我们检查我的列表,我们有

然后您可以根据需要添加或删除。

编辑

如果您想使用诸如“变量”1 之类的字符串名称来获取或设置这些值,我们需要稍微更改结构。

首先我们声明一个结构来保存成对的值。

    class Valuegroup
    {
        public int FirsValue { get; set; }
        public int SecondValue { get; set; }
    }

那么我们需要的是集合键值对,其中的键必须是唯一的。在.Net中我们可以使用Dictionary结构来实现。

我们声明如下:

        Dictionary<string, List<Valuegroup>> myList = new Dictionary<string, List<Valuegroup>>();

通过上述声明,我们将能够使用键字符串变量 1 、变量 2 、变量 3 等作为唯一键。然后我们可以将我们的值作为组添加到List&lt;Valuegroup&gt; 中。每个列表元素将代表一组项目,例如 (2, 4) 、 (5, 1) 等。

把它们放在一起:

    class Valuegroup
    {
        public int FirsValue { get; set; }
        public int SecondValue { get; set; }
    }
    static void Main(string[] args)
    {
        //Variable 1 = (2, 4) , (5, 1)

        Valuegroup first = new Valuegroup
        {
            FirsValue = 2,
            SecondValue = 4
        };

        Valuegroup second = new Valuegroup
        {
            FirsValue = 5,
            SecondValue = 1
        };

        Dictionary<string, List<Valuegroup>> myList = new Dictionary<string, List<Valuegroup>>();
        myList.Add("Variable 1",new List<Valuegroup>{first,second});

        //retrive a value using a key name eg Variable 1
        List<Valuegroup> temp = new List<Valuegroup>();
        myList.TryGetValue("Variable 1", out temp);

        //do a search using Linq
        var t = myList.Where(x => x.Key == "Variable 2");
    } 

这显示了 myList 包含的内容

在这里您可以看到使用键 "Variable 1" 获取值返回正确的结果。同时搜索"Variable 2" 没有结果。

【讨论】:

  • 嗨,Alex,我可以在列表上执行搜索吗?在上面的示例中,我可以使用变量名称进行搜索,例如对变量 1、变量 4、变量 5、变量 7 中的元素进行常见搜索应该返回结果(2,4)。如果你能告诉我一个小例子是可能的,我会继续这样做。只是想确保如果我使用列表,我的所有要求都会得到满足。如果可能的话,请用代码澄清我。
  • @Deepak 我对提议的解决方案进行了一些更改。这应该为您提供解释和解决问题的方法。
猜你喜欢
  • 1970-01-01
  • 2022-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多