Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中创建二维动态数组?_C#_Arrays - Fatal编程技术网

如何在C#中创建二维动态数组?

如何在C#中创建二维动态数组?,c#,arrays,C#,Arrays,如何定义二维动态数组?我在写Dijkstra算法程序,我希望每个连接到另一个节点的节点将其值保存到一个数组中。事实上,它应该是一个动态的二维数组。未指定哪个节点连接到另一个节点 我已经有一段时间没有完成Dijkstra算法的实现了,但是您可以用几种方式表示数据。例如,您可以有一个列表列表,也可以有一个列表数组。然后,可以将数组索引视为顶点标签,将列表视为与其连接的数组列表 public class Node { public int Weight { get; set; } publ

如何定义二维动态数组?我在写Dijkstra算法程序,我希望每个连接到另一个节点的节点将其值保存到一个数组中。事实上,它应该是一个动态的二维数组。未指定哪个节点连接到另一个节点


我已经有一段时间没有完成Dijkstra算法的实现了,但是您可以用几种方式表示数据。例如,您可以有一个列表列表,也可以有一个列表数组。然后,可以将数组索引视为顶点标签,将列表视为与其连接的数组列表

public class Node
{
   public int Weight { get; set; }
   public int Connected { get; set; }
}

// You can use either an array of lists or a list of lists
List<List<Node>> graph = new List<List<Node>>();
        // The index is the edge label - e.g. arr[0] is the edge labeled "0"
        graph[0] = new List<Node>()
        {
            new Node() { Weight = 175, Connected = 1 },
            new Node() { Weight= 100, Connected = 2 }
            // Etc...
        };
        graph[1] = new List<Node>()
        {
            // Basically, to represent an undirected edge you're representing two weighted edges
            // (i.e. a connection from 1 -> 2 and a connection from 2 -> 1)
            // This also makes directed edges easy to represent
            new Node() { Weight = 175, Connected = 1 }
            // Etc...
        };
公共类节点
{
公共整数权重{get;set;}
公共int连接{get;set;}
}
//您可以使用列表数组或列表列表
列表图=新列表();
//索引是边缘标签-例如,arr[0]是标记为“0”的边缘
图[0]=新列表()
{
新节点(){Weight=175,Connected=1},
新节点(){Weight=100,Connected=2}
//等等。。。
};
图[1]=新列表()
{
//基本上,要表示一条无向边,您需要表示两条加权边
//(即,来自1->2的连接和来自2->1的连接)
//这也使得有向边易于表示
新节点(){Weight=175,Connected=1}
//等等。。。
};

例如,您还可以拥有顶点列表(或集合)和边列表。(事实上,在形式数学意义上,这是图形的实际定义-一组边和一组顶点)。

如果要查找动态数组,请使用列表对象。我对值进行了硬编码,但当需要动态对象时,可以使用Add()方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {

            List<Node> graph = new List<Node>() {
                new Node() { 
                    id = 0, neighbors = new List<KeyValuePair<int,int>>() {
                       new KeyValuePair<int,int>( 1,127),
                       new KeyValuePair<int,int>( 2,100),
                       new KeyValuePair<int,int>( 4,139),
                       new KeyValuePair<int,int>( 6,117),
                       new KeyValuePair<int,int>( 7,156)
                    }
                },
                new Node() { 
                    id = 1, neighbors = new List<KeyValuePair<int,int>>() {
                       new KeyValuePair<int,int>( 0,127),
                       new KeyValuePair<int,int>( 2,102),
                       new KeyValuePair<int,int>( 3,108),
                       new KeyValuePair<int,int>( 7,53)
                    }
                },
                new Node() { 
                    id = 2, neighbors = new List<KeyValuePair<int,int>>() {
                       new KeyValuePair<int,int>( 0,100),
                       new KeyValuePair<int,int>( 1,102),
                       new KeyValuePair<int,int>( 3,111),
                       new KeyValuePair<int,int>( 4,173),
                       new KeyValuePair<int,int>( 5,175)
                    }
                },            
                new Node() { 
                    id = 3, neighbors = new List<KeyValuePair<int,int>>() {
                       new KeyValuePair<int,int>( 1,108),
                       new KeyValuePair<int,int>( 2,111)
                    }
                },            
                new Node() { 
                    id = 4, neighbors = new List<KeyValuePair<int,int>>() {
                       new KeyValuePair<int,int>( 0,139),
                       new KeyValuePair<int,int>( 2,173),
                       new KeyValuePair<int,int>( 6,165)
                    }
                },            
                new Node() { 
                    id = 5, neighbors = new List<KeyValuePair<int,int>>() {
                       new KeyValuePair<int,int>( 2,175),
                       new KeyValuePair<int,int>( 6,95),
                       new KeyValuePair<int,int>( 7,145)
                    }
                },            
                new Node() { 
                    id = 6, neighbors = new List<KeyValuePair<int,int>>() {
                       new KeyValuePair<int,int>( 0,117),
                       new KeyValuePair<int,int>( 4,165),
                       new KeyValuePair<int,int>( 5,95)
                    }
                },            
                new Node() { 
                    id = 7, neighbors = new List<KeyValuePair<int,int>>() {
                       new KeyValuePair<int,int>( 0,156),
                       new KeyValuePair<int,int>( 1,53),
                       new KeyValuePair<int,int>( 5,145)
                    }
                }            
            };
        }
    }
    public class Node
    {
        public int id { get; set; }
        public List<KeyValuePair<int, int>> neighbors { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
命名空间控制台应用程序9
{
班级计划
{
静态void Main(字符串[]参数)
{
列表图=新列表(){
新节点(){
id=0,邻居=新列表(){
新的KeyValuePair(1127),
新的KeyValuePair(2100),
新的KeyValuePair(4139),
新的KeyValuePair(6117),
新的KeyValuePair(7156)
}
},
新节点(){
id=1,邻居=新列表(){
新的KeyValuePair(0127),
新的KeyValuePair(2102),
新的KeyValuePair(3108),
新的KeyValuePair(7,53)
}
},
新节点(){
id=2,邻居=新列表(){
新的KeyValuePair(0100),
新的KeyValuePair(1102),
新的KeyValuePair(3111),
新的KeyValuePair(4173),
新的KeyValuePair(5175)
}
},            
新节点(){
id=3,邻居=新列表(){
新的KeyValuePair(1108),
新的KeyValuePair(2111)
}
},            
新节点(){
id=4,邻居=新列表(){
新的KeyValuePair(0139),
新的KeyValuePair(2173),
新的KeyValuePair(6165)
}
},            
新节点(){
id=5,邻居=新列表(){
新的KeyValuePair(2175),
新的KeyValuePair(6,95),
新的KeyValuePair(7145)
}
},            
新节点(){
id=6,邻居=新列表(){
新的KeyValuePair(0117),
新的KeyValuePair(4165),
新的KeyValuePair(5,95)
}
},            
新节点(){
id=7,邻居=新列表(){
新的KeyValuePair(0156),
新的KeyValuePair(1,53),
新的KeyValuePair(5145)
}
}            
};
}
}
公共类节点
{
公共int id{get;set;}
公共列表邻居{get;set;}
}
}

您可以简单地使用[]:

double[][] x = new double[5][];

x[0] = new double[10];
x[1] = new double[5];
x[2] = new double[3];
x[3] = new double[100];
x[4] = new double[1];
第二种选择是使用:

List<List<double>> 2darray = new List<List<double>> ();
2darray[0] = new List<double>();
2darray[0][0] = 1;
List 2darray=newlist();
2darray[0]=新列表();
2darray[0][0]=1;

听起来你想要一个
列表
。Double在这里不起作用-你需要能够存储边权重和连接到的边。另外,您不能使用数组数组,因为它的大小是静态分配的(问题是如何动态分配)。非常感谢您的回答,但没有指定节点数可能是节点数7、100或1000。我的意思是这是代码:List[]arr=new List[7];是的,您可以执行列表列表、数组列表或列表数组(取决于您事先掌握的信息量)。如果您希望这样做,还可以在运行时分配数组的大小。在您的情况下(因为您指定了它们应该是动态的),您可能需要创建一个列表列表。使用列表列表时,代码实际上或多或少是相同的(您仍然可以执行索引之类的操作)。我不知道运行时数据的大小。因为它的节点可能会逐渐增加,例如:最初的节点可能有2、…5或6,。。。然后连接到另一个节点,我决定添加新的节点。在这种情况下,您肯定希望使用列表列表,而不是