Tag: 数组

C#:自定义数组排序

我想在一个目录中对一个字符串数组进行排序,给定一个自定义映射(它实际上是基于其扇区的股票名称排序)。 我不确定用于表示映射的数据结构,以及如何编写自定义排序方法。 例如,假设我有以下字符串数组: string[] fileNames = “bac.csv”, “c.csv”, “cvx.csv”, “java.csv”, “msft.csv”, “xom.csv”; 以下是映射: {“bac”, “c”} => 0 {“msft”, “java”} => 1 {“xom”, “cvx”} => 2 我想string [] customSort(string [] fileNames)返回以下内容: “bac.csv”, “c.csv”, “java.csv”, “msft.csv”, “xom.csv”, “cvx.csv” 您将使用什么数据结构来表示映射,以及编写排序方法的优雅方式是什么?

如何实现二维矩阵的Kadane算法

我试图弄清楚如何为Kadane的2D Matrix算法实现C#代码。 我在这里找到了一个版本: Kadane的算法用于查找具有最大总和的子arrays 但我想要一个2D版本。 基本上,给定正数和负数的矩阵N×N,我需要找到一个子矩阵,其中所有元素的总和将是最大的。

检查数组是否包含与其他数组完全相同的序列

我有一个问题,我需要检查一些数组是否是更大数组的一部分,这将是相当容易的但我需要检查更大的数组是否包含完全相同的序列。 例如 int[] greaterArray = {8, 3, 4, 5, 9, 12, 6 … n – elements} int[] lesserArray = {3, 4, 5} 现在我需要知道较小的数组是否是这个数组的一部分,但具有相同的序列,所以它在更大的数组中包含3,4,5彼此相邻。 我试过了: var exists = greaterArray.Intersect(lesserArray).Any(); 但是如果较小数组的任何元素存在于更大的数组中,而不是精确的序列,它会返回给我信息。 有任何想法吗?

列表数组抛出NullReferenceException

我正在尝试制作一个列表列表。 我这样做: public static List[] words = new List[30]; public static List[] hints = new List[30]; 我称之为: foreach (string item in vars.directory) { reader2 = new StreamReader(item); while (reader2.Peek() > 0) { string line = reader2.ReadLine(); if (line.StartsWith(“#”)) { vars.words[counter].Add(line.Substring(1, line.Length – 1)); //here } else if (line.StartsWith(“-“)) { vars.hints[counter].Add(line.Substring(1, line.Length – 1)); //another here […]

如何保留一些锯齿状数组的列和行,并删除c#中不需要的列和行

我有跟随锯齿状的数组 int[][] dists1 = new int[][] { new int[]{0,2,3,5,2,4}, new int[]{2,0,1,3,5,3}, new int[]{3,1,0,4,4,3}, new int[]{5,3,4,0,2,4}, new int[]{2,5,4,2,0,2}, new int[]{4,3,3,4,2,0} }; 我学会了如何删除一个特定的列和行(例如3)。 这是一个链接 。 现在我希望动态编程如下:考虑这个数组 int[] day={1,4,5}; 这个数组可以是另一个数组( 因为这个原因,我使用了动态术语 ) 数组“day”的元素显示“dists1”矩阵行和列,所以我希望名为“dists2”的新锯齿状数组只包含1,4,5 行和列“0”的列和行, 因为它是固定的行和列如下: int[][] dists2 = new int[][] { new int[]{0,2,2,4}, new int[]{2,0,5,3}, new int[]{2,5,0,2}, new int[]{4,3,2,0} };

从C#传递Bool数组到C ++代码

我目前正在和一些其他人一起开展一个项目,他们很遗憾不再提供参考资料。 该项目使用C#作为GUI,使用C ++更新我们的古老数据库。 目前,我在C#中有这个function: [DllImport(“Synch.DLL”, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] //Synch bool Synch(void * voidPtr, const TCHAR * databaseDir, const TCHAR * truckDir) static extern Int32 Synch(IntPtr pSync, string databaseDir, string destDir, ref bool[] wholeFiles); wholeFiles是一个布尔数组,用于根据用户输入确定是否应同步某些文件。 C ++应该接受这个数组,并且能够在满足某些条件时将一些bool翻转为true或false。 if (numSyncRecords > 0){ Log::WriteVerbose(_T(” %d recs, %d differences found”), (int) numRecsUsed, (int) numSynRecords); if(wholeFiles[sourceFileID]){ CString […]

是否有类似动态数组的List 允许访问.NET中的内部数组数据?

查看List的来源,似乎没有好的方法来访问私有_items项目数组。 我需要的基本上是一个动态的struct 列表 ,然后我可以在适当的位置进行修改。 根据我的理解,因为C#6还不支持ref返回类型,所以你不能让List返回对元素的引用,这需要复制整个项目,例如: struct A { public int X; } void Foo() { var list = new List { new A { X = 3; } }; list[0].X++; // this fails to compile, because the indexer returns a copy // a proper way to do this would be var copy = list[0]; copy.X++; […]

MarshalAsAttribute字符串数组

我试图从Beckhoff-PLC读取报警结构到ac#class。 首先,我必须在c#中使用完全相同的结构,它目前看起来像这样: [StructLayout(LayoutKind.Sequential, Pack = 1)] public class Alarm { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 81)] public string text; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)] public string objectName; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] public string[] instancePath = new string[6]; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)] public string timeStamp; public int priority; [MarshalAs(UnmanagedType.I1)] public bool acknowledge; [MarshalAs(UnmanagedType.I1)] public bool disabled; [MarshalAs(UnmanagedType.I1)] public bool […]

展平可能包含数组的对象数组

我有一个IEnumerable ,它可能包含也可能不包含一些嵌套集合。 例如,我的起点可能如下所示: [ “foo”, 2, [1, 2, 3, 4], “bar” ] 我想把它压平为: [ “foo”, 2, 1, 2, 3, 4, “bar” ] 我在想SelectMany应该在这里工作但是找不到合适的组合。 我可以蛮力,但我认为应该有一个更优雅的解决方案。

如何将1D字节数组转换为包含位图的2D字节数组?

我已经使用了LockBits和UnlockBits函数,并将Image的字节数组转换为1D数组。 (仅考虑黑白/二值化图像) 有没有办法将它带到2Darrays(图像高度和宽度的大小)? 所以我可以将数组转换为“.txt”文件并查看它? 我用来将图像转换为1D数组的代码如下: Public void function(Bitmap image){ { byte[] arr1D; byte[] arr2D; BitmapData data = image.LockBits(new Rectangle(0, 0, img_w, img_h), ImageLockMode.ReadOnly, image.PixelFormat); try { IntPtr ptr = data.Scan0; int bytes = Math.Abs(data.Stride) * image.Height; byte[] rgbValues = new byte[bytes]; arr1D = rgbValues; Marshal.Copy(ptr, rgbValues, 0, bytes); } finally { image.UnlockBits(data); } } 由于图像是二进制的,因此字节数组的值仅为255和0。 […]