以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- [求助]如何判断两个数组是否相等 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=157502) |
-- 作者:chnfo -- 发布时间:2020/10/18 22:26:00 -- [求助]如何判断两个数组是否相等 现在有两个数组 dim A as string() = {"A","B"} dim B as string() = {"A","C"} 如何判断这两个数组是否相等.遍历比较的方法效率不高(如果数组内元素较多的时候) 网上有用JSON.stringify()方法来转换比较 论坛里也有说用:Output.Show(Enumerable.SequenceEqual(A, B)) 也有:Output.Show(A.SequenceEqual(B)) 但实测这种方法有缺陷,就是完全区分大小写 Dim x As String() = {"A","B"} Dim y As String() = {"a","B"} Output.Show(Enumerable.SequenceEqual(x, y))-----执行结果是false,如果都改为大写,就true了 Output.Show(x.SequenceEqual(y)) -----执行结果是false,如果都改为大写,就true了 \' 也有说用Equals() 狐表有没有简单的函数或方法调用的? 看到有一个方法,代码比较长 Dim A As Integer() = {0,1,2} Dim B As Integer() = {0,1,2} Dim res As Integer = ctype(a , System.Collections.IstructuralComparable).CompareTo(b,Comparer(of Integer).Default) output.show(res)---返回0为相等,-1为不等 但这个方法只能比较数值型数组,string型的不可以 如果要判断集合相等,又要用什么方法呢? 当然了,有判断数组相等的方法调用,那就把集合转成数组再比较也行 [此贴子已经被作者于2020/10/18 22:52:50编辑过]
|
-- 作者:有点蓝 -- 发布时间:2020/10/18 22:39:00 -- Dim A As String() = {"0","1","2"} Dim B As String() = {"0","1","2"} Dim res As String= ctype(a , System.Collections.IstructuralComparable).CompareTo(b,Comparer(of String).Default) output.show(res)
|
-- 作者:chnfo -- 发布时间:2020/10/18 22:55:00 -- Dim A As String() = {"A"} Dim B As String() = {"A"} \'如果把它换成小写,运行结果就是1了 Dim res As String= ctype(a , System.Collections.IstructuralComparable).CompareTo(b,Comparer(of String).Default) output.show(res) 如果数组是字符型的,显示的结果是0和1 如果数组是数字型的,显示的结果是0和-1 还好是相等时,都是0,要是一个是1,一个是-1,才真的要死人了
[此贴子已经被作者于2020/10/18 23:00:14编辑过]
|
-- 作者:有点蓝 -- 发布时间:2020/10/19 -- Dim A As String() = {"0","A","2"} Dim B As String() = {"0","a","2"} Dim c As String = String.Join("|",a).ToLower Dim d As String = String.Join("|",b).ToLower Output.Show(c = d)
|
-- 作者:有点酸 -- 发布时间:2020/10/19 8:51:00 -- 自己做个内部函数,代码为: Dim A As object = args(0) Dim B As object = args(1) For i As Integer = 0 To Math.min(a.Length,b.Length) - 1 If a(i) > b(i) Then Return 1 ElseIf a(i) < b(i) Then Return - 1 End If Next Return 0 假定函数名为comparearray,你可以这样调用: Dim A As String() = {"0","A","2"} Dim B As String() = {"0","a","2"} Return Functions.Execute("comparearray",a,b) 数字也行: Dim A As Integer() = {1,2,3} Dim B As Integer() = {7,8,9} Return Functions.Execute("comparearray",a,b) |
-- 作者:chnfo -- 发布时间:2020/10/19 15:12:00 -- Dim xx As new Dictionary(of String(),String) xx.Add({"A","B"},"XYZ") xx.Add({"C","D"},"MNP") Dim yy As String() = {"A","B"} If xx.ContainsKey(yy) Then Output.Show(xx(yy)) Else Output.Show("没找到NO") End If 数组不能做键值么?
|
-- 作者:有点酸 -- 发布时间:2020/10/19 15:42:00 -- 键值都是简单数据 |