求一个二维数组查询方法

dream__life 2012-10-24 08:06:12
已知一个数组:

{
{"1","aaa"},
{"1","bbb"},
{"2","ccc"},
{"2","ddd"},
{"3","uuu"},
{"1","eee"},
{"4","aaa"},
{"4","qqq"}
}

想要得到如下效果:
1、查询出第一列相同的第二列值
2、在查出的第二列结果中如果有相同的值,则再次合并
3、如果第一列值只有一行,则忽略(例如:{"3","uuu"})

即得到的结果应该是:
aaa,bbb,eee,qqq

ccc,ddd
...全文
256 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2012-10-25
  • 打赏
  • 举报
回复
用LINQ 蛋疼了点
dream__life 2012-10-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
C# code
string[,] values = { { "1", "aaa" }, { "1", "bbb" }, { "2", "ccc" }, { "2", "ddd" }, { "3", "uuu" }, { "1", "eee" }, { "4", "aaa" }, { "4", "qqq" } };
SortedList<string, strin……
[/Quote]
2楼可以,有点BUG,sortedList的循环的做个递归就行了。
H463428621 2012-10-25
  • 打赏
  • 举报
回复
2楼真烦。。
XBodhi. 2012-10-25
  • 打赏
  • 举报
回复
搞那么 复杂 做什么, 你自己 创建一个 结构吗, 用 字典或是。


或者你模仿一个 Dictionary<K,V> 的形式。

kxloveh 2012-10-25
  • 打赏
  • 举报
回复
对效率有要求吗?没要求的话,2楼正解
SQL77 2012-10-25
  • 打赏
  • 举报
回复
namespace SumString
{
class Program
{
static string[,] str = new string[,] { { "1", "aaa" }, { "1", "bbb" }, { "2", "ccc" }, { "2", "ddd" },
{ "3", "uuu" }, { "1", "eee" }, { "4", "aaa" }, { "4", "qqq" } };
static List<string[,]> list = new List<string[,]>();
static List<string> listid = new List<string>();

static void Main(string[] args)
{
Addlist();
foreach (string[,] str in list)
{
if (!listid.Contains(str[0, 0]))
{
if(SumString(str).TrimEnd(new char[]{','}).Contains(","))
Console.WriteLine("{0}--{1}", str[0, 0], SumString(str).TrimEnd(new char[]{','}));
}
listid.Add(str[0,0]);

}
Console.ReadKey();

}
public static string SumString(string [,] para)
{
string str="";
foreach (string[,] s in list)
{
if (s[0, 0] == para[0,0])
{
str += s[0, 1] + ",";
}
}
return str;
}
public static void Addlist( )
{
string[,] tempstr = new string[1, 2];
for (int i = 0; i < str.GetLength(0); i++)
{
tempstr = new string[1, 2];
for (int j = 0; j < str.GetLength(1); j++)
{
if(j==0)
tempstr[0, 0] = str[i, j];
else
tempstr[0, 1] = str[i, j];

}
list.Add(tempstr);
}
}
}
}
q269399361 2012-10-25
  • 打赏
  • 举报
回复
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;

//namespace ConsoleApplication1
//{
// class Program
// {
// static void Main(string[] args)
// {
// string[,] strArray = new string[8, 2] {{"1","aaa"},{"1","bbb"},{"2","ccc"},{"2","ddd"},{"3","uuu"},{"1","eee"},{"4","aaa"},{"4","qqq"}};
// string[,] newstrArray=new string[8,2];
// string[,] newstrArray1 = new string[8, 2];
// bool b = true;
// //第一轮比较:查询出第一列相同的第二列值保存到数组newstrArray中
// for (int i = 0; i < strArray.GetLength(0); i++)
// {
// for (int k = 0; k < strArray.GetLength(0); k++)
// {
// if (i == k)
// {
// //自己不能和自己比较
// continue;
// }
// //如果第X行第一列相等,则保存数据到newstrArray数组中
// if (strArray[i, 0] == strArray[k, 0])
// {
// newstrArray[k, 0] = strArray[k, 0];
// newstrArray[k, 1] = strArray[k, 1];
// }
// }
// }
// Console.WriteLine("第一轮比较后的结果为:");
// for (int i = 0; i < strArray.GetLength(0); i++)
// {
// if (newstrArray[i, 0] == null || newstrArray[i, 0] == "")
// continue;
// for (int j = 0; j < strArray.GetLength(1); j++)
// {
// Console.Write(newstrArray[i, j] + " ");
// }
// Console.WriteLine();
// }
// //第二轮比较:在查出的第二列结果中如果有相同的值,则再次合并到数组newstrArray1中
// for (int i = 0; i < strArray.GetLength(0); i++)
// {
// for (int k = i + 1; k < strArray.GetLength(0); k++)
// {
// //外层循环一次内层循环一遍,如果发现有相同的则修改B为false;
// if (newstrArray[i,1]==newstrArray[k,1])
// {
// b = false;
// continue;
// }
// }
// //内层循环完后,判断如果B为真说明第二列没有相同的值,则进行保存
// if (b)
// {
// newstrArray1[i, 0] = newstrArray[i, 0];
// newstrArray1[i, 1] = newstrArray[i, 1];
// }
// //把B再次修改为true进行下一次判断
// b = true;
// }
// Console.WriteLine("第二轮比较后的结果为:");
// for (int i = 0; i < strArray.GetLength(0); i++)
// {
// if (newstrArray1[i, 0] == null || newstrArray1[i, 0] == "")
// continue;
// for (int j = 0; j < strArray.GetLength(1); j++)
// {
// Console.Write(newstrArray1[i, j] + " ");
// }
// Console.WriteLine();
// }
// }
// }
//}
用最笨的一种方法解决,刚接触编程的人也能看懂的代码
dalmeeme 2012-10-24
  • 打赏
  • 举报
回复
		string[,] values = { { "1", "aaa" }, { "1", "bbb" }, { "2", "ccc" }, { "2", "ddd" }, { "3", "uuu" }, { "1", "eee" }, { "4", "aaa" }, { "4", "qqq" } };
SortedList<string, string> sortedList = new SortedList<string, string>();
for (int i = 0; i < values.GetLength(0); i++)
{
if (sortedList.ContainsKey(values[i, 0]))
sortedList[values[i, 0]] += "," + values[i, 1];
else
sortedList.Add(values[i, 0], values[i, 1]);
}
for (int i = 0; i < sortedList.Count; i++)
if (!sortedList[sortedList.Keys[i]].Contains(','))
sortedList.RemoveAt(i--);
if (sortedList.Count > 1)
{
for (int i = 0; i < sortedList.Count; i++)
{
for (int j = i + 1; j < sortedList.Count; j++)
{
string[] values1 = sortedList[sortedList.Keys[i]].Split(',');
string[] values2 = sortedList[sortedList.Keys[j]].Split(',');
if (values1.Intersect(values2).Count() > 0)
{
sortedList[sortedList.Keys[i]] = string.Join(",", values1.Union(values2).ToArray());
sortedList.RemoveAt(j--);
}
}
}
}
foreach (KeyValuePair<string, string> pair in sortedList)
Console.WriteLine(pair.Value);

输出:

aaa,bbb,eee,qqq
ccc,ddd

哈哈,分全给我吧。
wuyq11 2012-10-24
  • 打赏
  • 举报
回复
string[,] array={{"1","aaa"},...};

var query=from i in Enumerable.Range(0, (int)array.GetLongLength(0))
from j in Enumerable.Range(0,(int)array.GetLongLength(1))
where array[i,j]=="1"
select array[i,j];
Console.WriteLine(query.Count());

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧