Arraylist

ktoya 2010-07-27 07:04:49
有2个ArrayList如下

ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();

al1.Add("a");
al1.Add("b");
al1.Add("c");
al1.Add("d");

al2.Add("a");
al2.Add("c");
al2.Add("zz");
al2.Add("a");
al2.Add("b");
al2.Add("a");
al2.Add("c");


现要求al1根据al2的值构建al3,当al1的值在al2中出现过2次或以上的,那么这个值就会被去掉:
如这个例子中a和c在al2中出现了2次或以上,就会被去掉,最后的结果应是 b,d

al1和al2都比较大,请教该如何有效的写这个函数?
...全文
202 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
i08kingdom 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wuyazhe 的回复:]
如果.net 3.5或更高,支持Linq还容易点:

C# code

static void Main(string[] args)
{
ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();

al1.Add("a");
al1.Add("b");
al1.……
[/Quote]

+1
ktoya 2010-07-27
  • 打赏
  • 举报
回复
只有.NET 2.0 所以采用了iammac的方法,谢谢大家
鸭梨山大帝 2010-07-27
  • 打赏
  • 举报
回复
一句话足矣


//lambda
al1.Cast<string>()
.Where(s => al2.Cast<string>()
.Count(a => a == s) <= 1)
.ToList()
.ForEach(s => Console.WriteLine(s));
teerhu 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 iammac 的回复:]
C# code

ArrayList al1 = new ArrayList() { "a", "b", "c", "d" };
ArrayList al2 = new ArrayList() { "a", "c", "zz", "a", "b", "a", "c" };
ArrayList rst = new Arr……
[/Quote]
up
QDJSC_2000 2010-07-27
  • 打赏
  • 举报
回复
使用LINQ很方便的,
同楼上代码:
static void Main(string[] args)
{
ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();

al1.Add("a");
al1.Add("b");
al1.Add("c");
al1.Add("d");

al2.Add("a");
al2.Add("c");
al2.Add("zz");
al2.Add("a");
al2.Add("b");
al2.Add("a");
al2.Add("c");

var different = al2.Cast<string>().Intersect(al1.Cast<string>()).Distinct();
var other = al2.Cast<string>().Except(different).Concat(different).OrderBy(s => s);
other.ToList().ForEach(s => Console.WriteLine(s));
Console.ReadKey();
}
Peter200694013 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wuyazhe 的回复:]
如果.net 3.5或更高,支持Linq还容易点:

C# code
static void Main(string[] args)
{
ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();

al1.Add("a");
al1.Add("b");
al1.Ad……
[/Quote]
推荐用Linq
兔子-顾问 2010-07-27
  • 打赏
  • 举报
回复
如果.net 3.5或更高,支持Linq还容易点:
static void Main(string[] args)
{
ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();

al1.Add("a");
al1.Add("b");
al1.Add("c");
al1.Add("d");

al2.Add("a");
al2.Add("c");
al2.Add("zz");
al2.Add("a");
al2.Add("b");
al2.Add("a");
al2.Add("c");

var different = al2.Cast<string>().Intersect(al1.Cast<string>()).Distinct();
var other = al2.Cast<string>().Except(different).Concat(different).OrderBy(s => s);
other.ToList().ForEach(s => Console.WriteLine(s));
Console.ReadKey();
}
shuai13869896140 2010-07-27
  • 打赏
  • 举报
回复
用遍历比较可行```
gxingmin 2010-07-27
  • 打赏
  • 举报
回复
你要是用list<string>,不用ArrayList,效率会高些
iammac 2010-07-27
  • 打赏
  • 举报
回复

ArrayList al1 = new ArrayList() { "a", "b", "c", "d" };
ArrayList al2 = new ArrayList() { "a", "c", "zz", "a", "b", "a", "c" };
ArrayList rst = new ArrayList();

al2.Sort();
int index = 0;
foreach (var n in al1)
{
index = al2.IndexOf(n);
if (index == -1 || index == al2.Count || al2[index + 1] != n)
rst.Add(n);
}
zhoubupt 2010-07-27
  • 打赏
  • 举报
回复
你的al1和al2在生成al3后还用么?不用就直接遍历al1,判断是否在al2中存在,如果大于两次,add入al3并remove掉,应该就是这思路吧
huminghua 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 foxdave 的回复:]

遍历al1
对于每个al1中的元素,如果al2中包含,则al2中remove掉这个元素,再次判断是否包含,如果包含则remove掉al1中的这个元素。

或者先排序一下 然后。。。
[/Quote]。。。。。。。。
Justin-Liu 2010-07-27
  • 打赏
  • 举报
回复
遍历al1
对于每个al1中的元素,如果al2中包含,则al2中remove掉这个元素,再次判断是否包含,如果包含则remove掉al1中的这个元素。

或者先排序一下 然后。。。

110,525

社区成员

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

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

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