在 C# 中对二维数组进行分组?

我有一个二维字符串数组:


string[,] arr = new string[3,3]{

  { "a", "b", "c" }, 

  { "a", "b", "c" }, 

  { "d", "e", "f" }, 

}

我想得到的结果是:


a,b,c = 2


d,e,f = 1


我试过:


List<List<string>> lLString = new List<List<string>>();

string[,] stringArray2D = new string[3, 3] {

    { "a", "b", "c" }, 

    { "a", "b", "c" }, 

    { "d", "e", "f" }, 

};

for (int i = 0; i < stringArray2D.GetLength(0); i++) {

    List<string> temp = new List<string>();

    for (int j = 0; j < stringArray2D.GetLength(1); j++) { 

        temp.Add(stringArray2D[i,j]);

    }

    lLString.Add(temp);

}

试图通过这样做来解决它:


lLString.GroupBy (ls => ls);


但似乎不对


ITMISS
浏览 332回答 3
3回答

蛊毒传说

您可以直接使用锯齿状数组,例如:string[][] arrayJagged = new[]{&nbsp; &nbsp; new[] { "a", "b", "c" },&nbsp; &nbsp; new[] { "a", "b", "c" },&nbsp; &nbsp; new[] { "d", "e", "f" },&nbsp; &nbsp; new[] { "g", "h", "i" },};但是你想在过去的废墟中挖掘并使用多维数组,只是为了让一切变得更加复杂。string[,] stringArray2D = new string[,]&nbsp;{&nbsp; &nbsp; { "a", "b", "c" },&nbsp; &nbsp; { "a", "b", "c" },&nbsp; &nbsp; { "d", "e", "f" },&nbsp; &nbsp; { "g", "h", "i" },};然后我们必须将多维数组转换为锯齿状数组:string[][] arrayJagged = new string[stringArray2D.GetLength(0)][];int length2 = stringArray2D.GetLength(1);for (int i = 0; i < arrayJagged.Length; i++){&nbsp; &nbsp; arrayJagged[i] = new string[length2];&nbsp; &nbsp; for (int j = 0; j < length2; j++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; arrayJagged[i][j] = stringArray2D[i, j];&nbsp; &nbsp; }}请注意,您实际上并不需要 a,List<List<string>>因为最终锯齿状数组的维度是预先确定的。然后你可以.GroupBy()使用锯齿状数组,并.Count()在每组上做一个:var grouped = arrayJagged.GroupBy(x => x, ArrayEqualityComparer<string>.Default)&nbsp; &nbsp; .Select(x => new&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; x.Key,&nbsp; &nbsp; &nbsp; &nbsp; Count = x.Count()&nbsp; &nbsp; })&nbsp; &nbsp; .ToArray();请注意,.NET 没有用于数组的默认相等比较器,因此您需要定义一个,以显示.GroupBy()如何检查元素的相等性:// Simple T[] IEqualityComparer<>public sealed class ArrayEqualityComparer<T> : IEqualityComparer<T[]>, IEqualityComparer{&nbsp; &nbsp; // One instance is more than enough&nbsp; &nbsp; public static readonly ArrayEqualityComparer<T> Default = new ArrayEqualityComparer<T>();&nbsp; &nbsp; // We lazily define it if necessary&nbsp; &nbsp; private readonly IEqualityComparer<T> equalityComparer;&nbsp; &nbsp; public ArrayEqualityComparer()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; equalityComparer = EqualityComparer<T>.Default;&nbsp; &nbsp; }&nbsp; &nbsp; public ArrayEqualityComparer(IEqualityComparer<T> equalityComparer)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.equalityComparer = equalityComparer;&nbsp; &nbsp; }&nbsp; &nbsp; /* IEqualityComparer<T[]> */&nbsp; &nbsp; public bool Equals(T[] x, T[] y)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (x == null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (y == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (y == null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return EqualsNotNull(x, y);&nbsp; &nbsp; }&nbsp; &nbsp; public int GetHashCode(T[] obj)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; unchecked&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int hash = 17;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (obj != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // This one will help distinguish between null and empty:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // hash(null) == 17, hash(empty) == 17 * 23&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hash = (hash * 23) + obj.Length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < obj.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hash = (hash * 23) + obj[i].GetHashCode();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return hash;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /* IEqualityComparer */&nbsp; &nbsp; bool IEqualityComparer.Equals(object x, object y)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (x == y)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (x == null || y == null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var x2 = x as T[];&nbsp; &nbsp; &nbsp; &nbsp; if (x2 == null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new ArgumentException("x");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var y2 = y as T[];&nbsp; &nbsp; &nbsp; &nbsp; if (y2 == null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new ArgumentException("y");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return EqualsNotNull(x2, y2);&nbsp; &nbsp; }&nbsp; &nbsp; int IEqualityComparer.GetHashCode(object obj)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; T[] obj2;&nbsp; &nbsp; &nbsp; &nbsp; if (obj != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj2 = obj as T[];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (obj2 == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new ArgumentException("obj");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj2 = null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return GetHashCode(obj2);&nbsp; &nbsp; }&nbsp; &nbsp; /* Implementation */&nbsp; &nbsp; private bool EqualsNotNull(T[] x, T[] y)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (x.Length != y.Length)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (x.Length != 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < x.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!equalityComparer.Equals(x[i], y[i]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }}

千巷猫影

创建一个覆盖等于的类public static void GroupList(){&nbsp; &nbsp; List<ListComp> lLString = new List<ListComp>();&nbsp; &nbsp; string[,] stringArray2D = new string[3, 3] { { "a", "b", "c" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ "a", "b", "c" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ "d", "e", "f" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; for (int i = 0; i<stringArray2D.GetLength(0); i++) {&nbsp; &nbsp; &nbsp; &nbsp; ListComp temp = new ListComp();&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j<stringArray2D.GetLength(1); j++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.Add(stringArray2D[i, j]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; lLString.Add(temp);&nbsp; &nbsp; }&nbsp; &nbsp; var gr = lLString.GroupBy(i => i);&nbsp; &nbsp; foreach (var g in gr)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Debug.WriteLine($"{g.Key} = {g.Count()}");&nbsp; &nbsp; }&nbsp; &nbsp; Debug.WriteLine("");}public class ListComp : List<string>{&nbsp; &nbsp; public override string ToString()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return string.Join(",", this);&nbsp; &nbsp; }&nbsp; &nbsp; public override bool Equals(object obj)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ListComp listComp = obj as ListComp;&nbsp; &nbsp; &nbsp; &nbsp; if (listComp == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Equals(listComp);&nbsp; &nbsp; }&nbsp; &nbsp; public bool Equals(ListComp listComp)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (listComp == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; return this.SequenceEqual(listComp);&nbsp; &nbsp; }&nbsp; &nbsp; public override int GetHashCode()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int hash = 1;&nbsp; &nbsp; &nbsp; &nbsp; foreach(string s in this)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hash *= s.GetHashCode();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return hash;&nbsp; &nbsp; }&nbsp; &nbsp; public ListComp (List<string> listComp)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.AddRange(listComp);&nbsp; &nbsp; }&nbsp; &nbsp; public ListComp()&nbsp; &nbsp; {&nbsp; &nbsp; }}

婷婷同学_

使用类似于您使用的循环的基本 for 循环创建字符串列表。然后使用GroupBy.string[,] arr = new string[3, 3]{&nbsp; &nbsp; &nbsp; &nbsp; { "a", "b", "c" },&nbsp; &nbsp; &nbsp; &nbsp; { "a", "b", "c" },&nbsp; &nbsp; &nbsp; &nbsp; { "d", "e", "f" },};int dim1 = arr.GetLength(0);int dim2 = arr.GetLength(1);List<string> temp = new List<string>();string st;for (int i = 0; i < dim1; i++){&nbsp; &nbsp; st = "";&nbsp; &nbsp; for (int j = 0; j < dim2; j++)&nbsp; &nbsp; &nbsp; &nbsp; st += arr[i, j] + ",";&nbsp; &nbsp; temp.Add(st);}var gr = temp.GroupBy(i => i);foreach (var g in gr)&nbsp; &nbsp; Console.WriteLine($"{g.Key} = {g.Count()}");输出:&nbsp;a,b,c, = 2d,e,f, = 1
打开App,查看更多内容
随时随地看视频慕课网APP