求助,关于String[]求重复值的算法

ts2112774 2009-01-19 02:24:13
string[] b = { "一", "七", "一", "三", "四","五","二", "六", "三", "四","一"};

怎么样得到值与该值所有对应位置

值:一 所在数组中位置:0,2,10

值:三 所在数组中位置:0,2

值:四 所在数组中位置:4,9
...全文
175 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiao2233 2009-01-19
  • 打赏
  • 举报
回复
public List<int> getIndex(string[] str, string s)
{
List<int> list2 = new List<int>();
for (int i = 0; i < str.Length; ++i)
{
if (str[i] == s)
{

list2.Add(i);
}
}
if (list2.Count == 0)
{
list2.Add(-1);
}
return list2;
}
static void Main(string[] args)
{
Hashtable dic = new Hashtable();
dic.
Program p = new Program();
int c = p.GetHashCode();
Console.WriteLine(c);


string[] b = { "一", "七", "一", "三", "四", "五", "二", "六", "三", "四", "一" };
string a = "一";

List<int> newL = new Program().getIndex(b, a);
if (newL[0] != -1)
{
Console.Write("值:" + a + " 所在数组中位置: ");
foreach (int i in newL)
{
Console.Write("{0} ", i);
}
}
else Console.WriteLine("b中没有a");
CutBug 2009-01-19
  • 打赏
  • 举报
回复
HashTable可以直接用索引访问,应该快点
 string[] b = { "一", "七", "一", "三", "四", "五", "二", "六", "三", "四", "一" };
Hashtable dic = new Hashtable();
for(int i=0;i<b.Length;i++)
{

string s = b[i];
if (dic[s] != null)
{
((List<string>)dic[s]).Add(i.ToString());
}
else
{
List<string> lPos = new List<string>();
lPos.Add(i.ToString());
dic.Add(s, lPos);
}

};

foreach (string s in dic.Keys)
{
List<string> l = (List<string>)dic[s];
if(l.Count>1)
{
Console.WriteLine("{0}:{1}",s,String.Join(",",l.ToArray()));
}
}

输出:
一:0,2,10
三:3,8
四:4,9
请按任意键继续. . .


xiaoyanwei2000 2009-01-19
  • 打赏
  • 举报
回复
写的太好了,学习学习
优途科技 2009-01-19
  • 打赏
  • 举报
回复
string[] b = { "一", "七", "一", "三", "四", "五", "二", "六", "三", "四", "一" };
Dictionary<string, List<int>> dic = new Dictionary<string, List<int>>();
for (int i = 0; i < b.Length; i++)
{
if (dic.ContainsKey(b[i]))
dic[b[i]].Add(i);
else
{
List<int> list = new List<int>();
list.Add(i);
dic.Add(b[i], list);
}
}
foreach (string s in dic.Keys)
{
Console.Write(s + ":");
foreach (int i in dic[s])
Console.Write(i + " ");
Console.WriteLine();
}
/*
输出:
一:0 2 10
七:1
三:3 8
四:4 9
五:5
二:6
六:7
*/

------
漂亮!
guilin_gavin 2009-01-19
  • 打赏
  • 举报
回复
这个应该不难吧……
chinaicm 2009-01-19
  • 打赏
  • 举报
回复

private void button1_Click(object sender, EventArgs e)
{
string[] b = { "一", "七", "一", "三", "四", "五", "二", "六", "三", "四", "一" };
string returnValue =GetIndex(b, "一");
}

public string GetIndex(string[] b,string value)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < b.Length; i++)
{
if (b[i] == value)
{
sb.Append(",");
sb.Append(i);
}
}
return sb.ToString().Substring(1);
}
mucy2008 2009-01-19
  • 打赏
  • 举报
回复
看看
我姓区不姓区 2009-01-19
  • 打赏
  • 举报
回复

string[] b = { "一", "七", "一", "三", "四", "五", "二", "六", "三", "四", "一" };
Dictionary<string, List<int>> dic = new Dictionary<string, List<int>>();
for (int i = 0; i < b.Length; i++)
{
if (dic.ContainsKey(b[i]))
dic[b[i]].Add(i);
else
{
List<int> list = new List<int>();
list.Add(i);
dic.Add(b[i], list);
}
}
foreach (string s in dic.Keys)
{
Console.Write(s + ":");
foreach (int i in dic[s])
Console.Write(i + " ");
Console.WriteLine();
}
/*
输出:
一:0 2 10
七:1
三:3 8
四:4 9
五:5
二:6
六:7
*/
feiyun0112 2009-01-19
  • 打赏
  • 举报
回复
Array.FindIndex(dinosaurs, FindStr1)
Array.FindIndex(dinosaurs,1, FindStr1)

private bool FindStr1(String s)
{
if (s=="一")
{
return true;
}
else
{
return false;
}
}


*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

http://feiyun0112.cnblogs.com/

111,131

社区成员

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

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

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