C# 将结构中定义的列表转换为Int[,]
我有点纠结于一个以特定方式定义的列表。情况就是这样: 我有一个名为Edge的结构,它由代码中的结构点(如下)中定义的两个点组成 我的输入列表称为“边缘”。使用该列表,我想产生两个输出: 列表点:包含列表“边”中显示的非重复点。 Int[,]edgeIndices:表示与输入列表“EDGE”相同的内容,但我要的不是这些点,而是之前创建的列表“points”中定义的这些点的索引。 你能帮我一下吗 请注意,使用列表和Int[,]而不是其他类型非常重要 非常感谢C# 将结构中定义的列表转换为Int[,],c#,arrays,list,struct,C#,Arrays,List,Struct,我有点纠结于一个以特定方式定义的列表。情况就是这样: 我有一个名为Edge的结构,它由代码中的结构点(如下)中定义的两个点组成 我的输入列表称为“边缘”。使用该列表,我想产生两个输出: 列表点:包含列表“边”中显示的非重复点。 Int[,]edgeIndices:表示与输入列表“EDGE”相同的内容,但我要的不是这些点,而是之前创建的列表“points”中定义的这些点的索引。 你能帮我一下吗 请注意,使用列表和Int[,]而不是其他类型非常重要 非常感谢 using System; using
using System;
using System.Collections.Generic;
namespace Test
{
struct Point
{
public readonly double X;
public readonly double Y;
public Point(double x, double y)
{
X = x;
Y = y;
}
}
struct Edge
{
public Point First;
public Point Second;
public Edge(Point First, Point Second)
{
this.First = First;
this.Second = Second;
}
}
class Program
{
static void Main(string[] args)
{
///////////////////////////INPUT////////////////////////////////////
var EDGES = new List<Edge>();
EDGES.Add(new Edge(new Point(5, 50), new Point(20, 100))); //EDGE 01
EDGES.Add(new Edge(new Point(20, 100), new Point(30, 50))); //EDGE 12
EDGES.Add(new Edge(new Point(30, 50), new Point(10, 0))); //EDGE 23
EDGES.Add(new Edge(new Point(5, 50), new Point(30, 50))); //EDGE 02
EDGES.Add(new Edge(new Point(5, 50), new Point(10, 0))); //EDGE 03
EDGES.Add(new Edge(new Point(20, 100), new Point(80, 100))); //EDGE 14
EDGES.Add(new Edge(new Point(10, 0), new Point(80, 100))); //EDGE 34
///////////////////////EXPECTED OUTPUTS/////////////////////////////
///
///POINTS (EXPECTED) as List<double[]>
///Index X Y
/// 0 5 50
/// 1 20 100
/// 2 30 50
/// 3 10 0
/// 4 80 100
///
///MULTIARRAY (EXPECTED)
///static int[,] edgeIndices =
/// {
/// {0, 1}, {1, 2}, {2, 3}, {0, 2},
/// {0, 3}, {1, 4}, {3, 4}
/// };
}
}
}
使用系统;
使用System.Collections.Generic;
名称空间测试
{
结构点
{
公共只读双X;
公共只读双Y;
公共点(双x,双y)
{
X=X;
Y=Y;
}
}
结构边
{
公共点优先;
公共点第二;
公共边缘(第一点,第二点)
{
这个。第一=第一;
这个。秒=秒;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
///////////////////////////输入////////////////////////////////////
var EDGES=新列表();
添加(新边(新点(5,50),新点(20,100));//边01
添加(新边(新点(20100),新点(30,50));//边12
添加(新边(新点(30,50),新点(10,0));//边23
添加(新边(新点(5,50),新点(30,50));//边02
添加(新边(新点(5,50),新点(10,0));//边03
添加(新边(新点(20100),新点(80100));//边14
添加(新边(新点(10,0),新点(80100));//边34
///////////////////////预期产出/////////////////////////////
///
///点(预期)如列表所示
///指数xy
/// 0 5 50
/// 1 20 100
/// 2 30 50
/// 3 10 0
/// 4 80 100
///
///多阵列(预期)
///静态整数[,]边标识=
/// {
/// {0, 1}, {1, 2}, {2, 3}, {0, 2},
/// {0, 3}, {1, 4}, {3, 4}
/// };
}
}
}
我建议您在List()中存储不同的点值。您可以使用以下代码来实现它:
using System;
using System.Collections.Generic;
namespace Test
{
struct Point
{
public readonly double X;
public readonly double Y;
public Point(double x, double y)
{
X = x;
Y = y;
}
}
struct Edge
{
public Point First;
public Point Second;
public Edge(Point First, Point Second)
{
this.First = First;
this.Second = Second;
}
}
class Program
{
static void Main(string[] args)
{
///////////////////////////INPUT////////////////////////////////////
var EDGES = new List<Edge>();
EDGES.Add(new Edge(new Point(5, 50), new Point(20, 100))); //EDGE 01
EDGES.Add(new Edge(new Point(20, 100), new Point(30, 50))); //EDGE 12
EDGES.Add(new Edge(new Point(30, 50), new Point(10, 0))); //EDGE 23
EDGES.Add(new Edge(new Point(5, 50), new Point(30, 50))); //EDGE 02
EDGES.Add(new Edge(new Point(5, 50), new Point(10, 0))); //EDGE 03
EDGES.Add(new Edge(new Point(20, 100), new Point(80, 100))); //EDGE 14
EDGES.Add(new Edge(new Point(10, 0), new Point(80, 100))); //EDGE 34
//FLL POINTS CACHE
var distinctPoints = new List<Point>();
foreach (Edge edge in EDGES)
{
if (!distinctPoints.Contains(edge.First))
distinctPoints.Add(edge.First);
if (!distinctPoints.Contains(edge.Second))
distinctPoints.Add(edge.Second);
}
//POINTS LIST OUTPUT
for (int i = 0; i < distinctPoints.Count; i++)
{
Console.WriteLine("{0} {1} {2}", i, distinctPoints[i].X, distinctPoints[i].Y);
}
//FILL 2D ARRAY OF INDICES
int[,] edgeIndices = new int[EDGES.Count, 2];
for (int i = 0; i < EDGES.Count; i++)
{
edgeIndices[i, 0] = distinctPoints.IndexOf(EDGES[i].First);
edgeIndices[i, 1] = distinctPoints.IndexOf(EDGES[i].Second);
}
//2D ARRAY OUTPUT
for (int i = 0; i < edgeIndices.GetLength(0); i++)
{
Console.WriteLine("({0}, {1})", edgeIndices[i, 0], edgeIndices[i, 1]);
}
Console.ReadKey();
}
}
}
使用系统;
使用System.Collections.Generic;
名称空间测试
{
结构点
{
公共只读双X;
公共只读双Y;
公共点(双x,双y)
{
X=X;
Y=Y;
}
}
结构边
{
公共点优先;
公共点第二;
公共边缘(第一点,第二点)
{
这个。第一=第一;
这个。秒=秒;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
///////////////////////////输入////////////////////////////////////
var EDGES=新列表();
添加(新边(新点(5,50),新点(20,100));//边01
添加(新边(新点(20100),新点(30,50));//边12
添加(新边(新点(30,50),新点(10,0));//边23
添加(新边(新点(5,50),新点(30,50));//边02
添加(新边(新点(5,50),新点(10,0));//边03
添加(新边(新点(20100),新点(80100));//边14
添加(新边(新点(10,0),新点(80100));//边34
//FLL点缓存
var distinctPoints=新列表();
foreach(边中的边)
{
如果(!distinctPoints.Contains(edge.First))
distinctPoints.Add(edge.First);
如果(!distinctPoints.Contains(edge.Second))
distinctPoints.Add(edge.Second);
}
//点列表输出
for(int i=0;i
以下是我的输出:
以下是我的实施:
using System;
using System.Collections.Generic;
namespace Test{
struct Point
{
public readonly double X;
public readonly double Y;
public Point(double x, double y)
{
X = x;
Y = y;
}
}
struct Edge
{
public Point First;
public Point Second;
public Edge(Point First, Point Second)
{
this.First = First;
this.Second = Second;
}
}
class Program
{
static void Main(string[] args)
{
///////////////////////////INPUT////////////////////////////////////
var EDGES = new List<Edge>();
EDGES.Add(new Edge(new Point(5, 50), new Point(20, 100))); //EDGE 01
EDGES.Add(new Edge(new Point(20, 100), new Point(30, 50))); //EDGE 12
EDGES.Add(new Edge(new Point(30, 50), new Point(10, 0))); //EDGE 23
EDGES.Add(new Edge(new Point(5, 50), new Point(30, 50))); //EDGE 02
EDGES.Add(new Edge(new Point(5, 50), new Point(10, 0))); //EDGE 03
EDGES.Add(new Edge(new Point(20, 100), new Point(80, 100))); //EDGE 14
EDGES.Add(new Edge(new Point(10, 0), new Point(80, 100))); //EDGE 34
var POINTS = new List<double[]>(EDGES.Count * 2);
FillPoints(EDGES, ref POINTS);
for (int i = 0; i < POINTS.Count; i++)
{
Console.WriteLine("{0} {1} {2}", i, POINTS[i][0], POINTS[i][1]);
}
Console.WriteLine();
var edgeIndices = new int[EDGES.Count, 2];
FillEdges(EDGES, POINTS, ref edgeIndices);
for (int i = 0; i < edgeIndices.GetLength(0); i++)
{
Console.WriteLine("({0}, {1})", edgeIndices[i, 0], edgeIndices[i, 1]);
}
Console.ReadKey(true);
}
static bool ListContainsPoint(List<double[]> POINTS, double[] POINT)
{
bool found = false;
for (int i = 0; i < POINTS.Count; i++)
{
var current = POINTS[i];
if (current[0] == POINT[0] && current[1] == POINT[1])
{
found = true;
break;
}
}
return found;
}
static int FindFirst(List<double[]> POINTS, double[] POINT)
{
int index = -1;
for (int i = 0; i < POINTS.Count; i++)
{
if (POINTS[i][0] == POINT[0] && POINTS[i][1] == POINT[1])
{
index = i;
break;
}
}
return index;
}
static void FillPoints(List<Edge> EDGES, ref List<double[]> POINTS)
{
for (int i = 0; i < EDGES.Count; i++)
{
var current = EDGES[i];
var firstPoint = new double[]{current.First.X, current.First.Y};
var secondPoint = new double[]{current.Second.X, current.Second.Y};
var firstCheck = ListContainsPoint(POINTS, firstPoint);
var secondCheck = ListContainsPoint(POINTS, secondPoint);
if (!firstCheck) POINTS.Add(firstPoint);
if (!secondCheck) POINTS.Add(secondPoint);
}
}
static void FillEdges(List<Edge> EDGES, List<double[]> POINTS, ref int[,] edgeIndices)
{
for (int i = 0; i < EDGES.Count; i++)
{
edgeIndices[i, 0] = FindFirst(POINTS, new double[] { EDGES[i].First.X, EDGES[i].First.Y });
edgeIndices[i, 1] = FindFirst(POINTS, new double[] { EDGES[i].Second.X, EDGES[i].Second.Y });
}
}
}
}
使用系统;
使用System.Collections.Generic;
名称空间测试{
结构点
{
公共只读双X;
公共只读双Y;
公共点(双x,双y)
{
X=X;
Y=Y;
}
}
结构边
{
公共点优先;
公共点第二;
公共边缘(第一点,第二点)
{
这个。第一=第一;
这个。秒=秒;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
///////////////////////////输入////////////////////////////////////
var EDGES=新列表();
添加(新边(新点(5,50),新点(20,100));//边01
添加(新边(新点(20100),新点(30,50));//边12
添加(新边(新点(30,50),新点(10,0));//边23
添加(新边(新点(5,50),新点(30,50));//边02
添加(新边(新点(5,50),新点(10,0));//边03
添加(新边(新点(20100),新点(80100));//边14
添加(新边(新点(10,0),新点(80100));//边34
变量点=新列表(EDGES.Count*2);
填充点(边、参考点);
对于(int i=0;i