Tag: 数组

在C#中从锯齿状数组转换为双指针

这里有一个简单的问题:有没有办法从锯齿状数组转换为双指针? 例如,将double[][]转换为double** 不幸的是,这不能仅仅通过强制转换(就像它可以在普通的旧C中一样)来完成。 使用fixed语句似乎也无法解决问题。 在C#中是否有任何(最好尽可能高效)的方法来实现这一目标? 我怀疑解决方案可能不是很明显,尽管我希望尽管是直截了当的。

为什么我不能将数组初始值设定项与隐式类型变量一起使用?

为什么我不能将数组初始值设定项与隐式类型变量一起使用? string[] words = { “apple”, “strawberry”, “grape” }; // legal string[] words = new string[]{ “apple”, “strawberry”, “grape” }; // legal var words = new []{ “apple”, “strawberry”, “grape” }; // legal var words = new string[]{ “apple”, “strawberry”, “grape” }; // legal var words = { “apple”, “strawberry”, “grape”, “peach” }; // ILLEGAL […]

在C#中进行冒泡排序的最优雅方式是什么?

可以清理吗? using System; class AscendingBubbleSort { public static void Main() { int i = 0,j = 0,t = 0; int []c=new int[20]; for(i=0;i<20;i++) { Console.WriteLine("Enter Value p[{0}]:", i); c[i]=int.Parse(Console.ReadLine()); } // Sorting: Bubble Sort for(i=0;i<20;i++) { for(j=i+1;jc[j]) { Console.WriteLine(“c[{0}]={1}, c[{2}]={3}”, i, c[i], j, c[j]); t=c[i]; c[i]=c[j]; c[j]=t; } } } Console.WriteLine(“bubble sorted array:”); // sorted […]

来自文本文件的2d数组c#

我有一个看起来像这样的文本文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 […]

C#generics中的通配符等价物

假设我有一个generics类,如下所示: public class GeneralPropertyMap { } 在其他一些类中,我有一个方法,它接受一个GeneralPropertyMap数组。 在Java中,为了获取包含任何类型的GeneralPropertyMap的数组,该方法将如下所示: private void TakeGeneralPropertyMap(GeneralPropertyMap[] maps) { } 我们使用通配符,以便稍后我们可以调用TakeGeneralPropertyMap传递一堆具有T任何类型的GeneralPropertyMap ,如下所示: GeneralPropertyMap[] maps = new GeneralPropertyMap[3]; maps[0] = new GeneralPropertyMap(); maps[1] = new GeneralPropertyMap(); maps[2] = new GeneralPropertyMap(); //And finally pass the array in. TakeGeneralPropertyMap(maps); 我试图在C#中找出一个没有成功的等价物。 有任何想法吗?

如果没有针对每个文本框的唯一测试,如何为null或为空,我如何检查多个文本框?

我在表单上有大约20个文本字段,用户可以填写。 我想提示用户在任何文本框中输入任何内容时都要考虑保存。 现在,对它的测试真的很长而且很混乱: if(string.IsNullOrEmpty(txtbxAfterPic.Text) || string.IsNullOrEmpty(txtbxBeforePic.Text) || string.IsNullOrEmpty(splitContainer1.Panel2) ||…//many more tests 有没有办法可以使用像任何数组的数组,其中数组由文本框组成,我检查它? 还有哪些方法可以非常方便地查看自程序启动以来是否有任何更改? 我应该提到的另一件事是有一个日期时间选择器。 我不知道我是否需要测试它,因为datetimepicker永远不会为null或为空。 编辑:我将答案纳入我的程序,但我似乎无法使其正常工作。 我按如下所示设置测试并继续触发Application.Exit()调用。 //it starts out saying everything is empty bool allfieldsempty = true; foreach(Control c in this.Controls) { //checks if its a textbox, and if it is, is it null or empty if(this.Controls.OfType().Any(t => string.IsNullOrEmpty(t.Text))) { //this means soemthing was […]

将MatchCollection转换为字符串数组

有没有比这更好的方法将MatchCollection转换为字符串数组? MatchCollection mc = Regex.Matches(strText, @”\b[A-Za-z-‘]+\b”); string[] strArray = new string[mc.Count]; for (int i = 0; i < mc.Count;i++ ) { strArray[i] = mc[i].Groups[0].Value; } PS: mc.CopyTo(strArray,0)抛出exception: 源数组中至少有一个元素无法转换为目标数组类型。

在数组中查找重复的整数并显示它们出现的次数

我正在编写一个代码,用于打印出数组中重复的整数及其出现次数。 我不允许使用LINQ,只是一个简单的代码。 我想我是如此接近但对如何获得正确的输出感到困惑: class Program { static void Main(string[] args) { int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 }; int count = 1; for (int i = 0; i < array.Length; i++) { for (int j = i; j < array.Length – […]

C#解析JSON对象数组

我有一个像json格式的对象数组: {“results”:[{“SwiftCode”:””,”City”:””,”BankName”:”Deutsche Bank”,”Bankkey”:”10020030″,”Bankcountry”:”DE”},{“SwiftCode”:””,”City”:”10891 Berlin”,”BankName”:”Commerzbank Berlin (West)”,”Bankkey”:”10040000″,”Bankcountry”:”DE”}]} 我想得到的是C#中的object[] ,其中一个对象包含一个json对象中的所有数据。 问题是,我不能像这里一样创建一个具有此对象属性的类: public class Result { public int SwiftCode { get; set; } public string City { get; set; } // . // . public string Bankcountry { get; set; } } 因为我每次都得到不同的结果,但我知道它总是一个对象数组。 有人知道我怎么能设法得到一个对象数组? 编辑 我必须通过WriteObject(results)将此对象传递给powershell。 所以输出应该只是array的对象。

对两个数组(值,键)进行排序,然后对键进行排序

我有一个似乎是一个简单的问题,但到目前为止我无法弄明白。 说我有两个数组: int[] values = {10,20,20,10,30}; int[] keys = {1,2,3,4,5}; Array.Sort(values,keys); 然后数组看起来像这样: values = {10,10,20,20,30}; keys = {4,1,2,3,5}; 现在,我想要做的是使键也以第二优先级排序,因此键数组看起来像这样: keys = {1,4,2,3,5}; 请注意,切换了1和4值,并且值数组的顺序没有改变。