删除ArrayList中的重复BitArrays [英] Removing duplicate BitArrays in an ArrayList

查看:71
本文介绍了删除ArrayList中的重复BitArrays的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将BitArrays添加到ArrayList然后删除任何重复项

- 有人可以帮我转发。我似乎有(至少;-))2

问题/缺乏理解(参见下面的测试代码):

(a)将BitArrays添加到ArrayList时然后循环遍历

ArrayList我似乎只访问最新添加的BitArray,而且我并不完全是

清除访问ArrayList中每个BItArray的最佳方法/>

(b)当我尝试删除重复的BItArrays时,我似乎只是将

当前索引标识为重复。 (我假设IndexOf和Equals

它使用的应该在这里起作用。)


提前谢谢,

Vjeko <使用System;

使用System.Collections;

命名空间RemoveDuplicateBitArrays

{

class Class1

{

static void Main(string [] args)

{

ArrayList arr = new ArrayList();

BitArray myBA1 = new BitArray(4);

myBA1.SetAll(false);


myBA1.Set(0,false);

myBA1.Set(1,false);

myBA1.Set(2,false);

myBA1.Set(3,true);

arr.Add(myBA1);


myBA1.SetAll(false);

myBA1.Set(0,false);

myBA1.Set(1,true);

myBA1.Set(2,false);

myBA1.Set(3,false);

arr.Add(myBA1);

i = 0;

foreach(BitArray x in arr)

{

Console.WriteLine(i ++);

DisplayBitArray(x);

}


// A.访问ArrayList中BitArrays的替代方法

// for(int j = 0; J&≤(arr.Count-1); j ++)

// {

// Console.WriteLine(" Array element is {0}",????);不确定

如何

//在这里访问bitarray的位

//}


Console.ReadLine();

} // Main


public static void DisplayBitArray(IEnumerable myList)

{

int i = 0;

System.Collections.IEnumerator myEnumerator =

myList.GetEnumerator();

while(myEnumerator) .MoveNext())

{

Console.WriteLine(" \t [{0}]:\ t {1}",i ++,

myEnumerator.Current);

Console.WriteLine();

}

} // DisplayBitArray


public static void RemoveDuplicateBitArraysinArrayList(ArrayList arr)

{

//逐步通过ArrayList这样

//采用BitArray元素并对每个元素进行XOR进一步向下

//如果结果为false则为ArrayList,将其删除,因为它是重复的


int index;


for(int i = 0; i<(arr.Count-1); i ++)

{

index = arr.IndexOf(arr [i],i + 1);

while(index> i)

{

arr.Remove(index);

if(i<(arr.Count-1))

{

index = arr.IndexOf(arr [i],i + 1);

}

else

{

index = -1;

}

}

}

} // RemoveDuplicateBitArraysinArrayList


} // Class1


} //名称空间RemoveDuplicateBitArrays

解决方案

vbportal,


问题是你要一遍又一遍地添加相同的位阵列

。你需要做的是在再次设置

值之前创建一个新的位数组,如下所示:


ArrayList arr = new ArrayList();

BitArray myBA1 = new BitArray(4);

myBA1.SetAll(false);


myBA1.Set(0,false );

myBA1.Set(1,false);

myBA1.Set(2,false);

myBA1.Set(3, true);

arr.Add(myBA1);


//需要在这里创建一个新的,或者你将添加相同的引用

数组列表。

myBA1 = new BitArray(4);

myBA1.SetAll(false);


//正常设置值。

myBA1.SetAll(false);

myBA1.Set(0,false);

myBA1 .Set(1,true);

myBA1.Set(2,false);

myBA1.Set(3,false);

arr.Add(myBA1);


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP ]

- mv*@spam.guard.caspershouse.com

vbportal < VB ****** @ discussions.microsoft.com>在留言中写道

新闻:E2 ********************************** @ microsof t.com ...


我想将BitArrays添加到ArrayList然后删除任何
重复
- 有人可以帮我转发。我似乎有(至少;-))2
问题/缺乏理解(参见下面的测试代码):
(a)将BitArrays添加到ArrayList然后循环通过
ArrayList我似乎只能访问最新添加的BitArray,而且我不能确切地知道在ArrayList中访问每个BItArray的最佳方式

(b)当我尝试为了删除重复的BItArrays,我似乎只是将
当前索引标识为重复。 (我假设IndexOf和
Equals
它使用的应该在这里起作用。)

提前致谢,
Vjeko

使用系统;
使用System.Collections;
命名空间RemoveDuplicateBitArrays
{Class class
{
static void Main(string [] args)
{
ArrayList arr = new ArrayList();
BitArray myBA1 = new BitArray(4);
myBA1.SetAll(false);

myBA1.Set(0 ,false);
myBA1.Set(1,false);
myBA1.Set(2,false);
myBA1.Set(3,true);
arr.Add (myBA1);

myBA1.SetAll(false);
myBA1.Set(0,false);
myBA1.Set(1,true);
myBA1 .Set(2,false);
myBA1.Set(3,false);
arr.Add(myBA1);

i = 0;
foreach(BitArray) x in arr)
{
Con sole.WriteLine(i ++);
DisplayBitArray(x);
}
//访问ArrayList中BitArrays的替代方法
// for(int j = 0; J&≤(arr.Count-1); j ++)
// {
// Console.WriteLine(" Array element is {0}",????);不确定
//如何在这里访问bitarray
//}

Console.ReadLine();
} //主要

public static void DisplayBitArray(IEnumerable myList)
{i / 0;
\\ n> System.Collections.IEnumerator myEnumerator =
myList.GetEnumerator();
while(myEnumerator.MoveNext())
{
Console.WriteLine(" \t [{0}]:\ t {1}",i ++,
myEnumerator.Current);
Console.WriteLine();
}
} // DisplayBitArray

public static void RemoveDuplicateBitArraysinArrayList(ArrayList
arr)
{
//逐步执行ArrayList,以便获取// BitArray元素并对每个元素进一步向下运算
//如果结果为false则将ArrayList删除,将其删除,因为它是复制

int index;

for(int i = 0; i<(arr.Count-1); i ++)
{= / index = arr.IndexOf(arr [i],i + 1);
while(index> i)
{
arr.Remove( index);
if(i<(arr.Count-1))
{
index = arr.IndexOf(arr [i],i + 1);
}

{
指数= -1;
}
}
}
} // RemoveDuplicateBitArraysinArrayList

} // Class1

} //名称空间RemoveDuplicateBitArrays



嗨尼古拉斯 - 谢谢你的答案 - 这解决了我的第一个问题。

我仍​​然不确定如何解决(b)即删除重复

BitArrays

(参见下面的子例程RemoveDuplicateBitArrays)


我从网上捏了这个代码 - 这里我假设index = arr.IndexOf(arr [i],

i + 1); ;将检查重复的BitArrays(当我读取IndexOf时使用

Equals进行相等性检查)从当前的ArrayList索引开始,并且

先行一步直到ArrayList结束(重复是通过

index识别并删除 - 但它不起作用 - 任何线索(我必须承认我非常ñ> b $ b新的C#和我的调试技能远远不够 - 但我们必须在某处开始

;-)?


如果不是太麻烦你也可以指出是否从集合的角度看,BitArrays如何适合ArrayLists,这是一个很好的信息来源

- 我似乎有一个问题,理解如何解决BItArray位/

IEnumerator是如何联系的(已经检查了很多书等但是没有找到

a令人满意的解释。


谢谢,Vjeko


Nicholas Paldino [.NET / C#MVP]"写道:

vbporta l,

问题是你要一遍又一遍地添加相同的位阵列。您需要做的是在再次设置
值之前创建一个新的位数组,如下所示:

ArrayList arr = new ArrayList();
BitArray myBA1 = new BitArray (4);
myBA1.SetAll(false);

myBA1.Set(0,false);
myBA1.Set(1,false);
myBA1 .Set(2,false);
myBA1.Set(3,true);
arr.Add(myBA1);

//需要在这里创建一个新的,或者你将添加相同的引用到
数组列表。
myBA1 = new BitArray(4);
myBA1.SetAll(false);

//设置通常值。
myBA1.SetAll(false);
myBA1.Set(0,false);
myBA1.Set(1,true);
myBA1.Set(2, false);
myBA1.Set(3,false);
arr.Add(myBA1);

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

vbportal < VB ****** @ discussions.microsoft.com>在消息中写道
新闻:E2 ********************************** @ microsof t.com。 ..


我想将BitArrays添加到ArrayList然后删除任何重复的
- 有人可以帮我转发。我似乎有(至少;-))2
问题/缺乏理解(参见下面的测试代码):
(a)将BitArrays添加到ArrayList然后循环通过
ArrayList我似乎只能访问最新添加的BitArray,而且我不能确切地知道在ArrayList中访问每个BItArray的最佳方式

(b)当我尝试为了删除重复的BItArrays,我似乎只是将
当前索引标识为重复。 (我假设IndexOf和
Equals
它使用的应该在这里起作用。)

提前致谢,
Vjeko

使用系统;
使用System.Collections;
命名空间RemoveDuplicateBitArrays
{Class class
{
static void Main(string [] args)
{
ArrayList arr = new ArrayList();
BitArray myBA1 = new BitArray(4);
myBA1.SetAll(false);

myBA1.Set(0 ,false);
myBA1.Set(1,false);
myBA1.Set(2,false);
myBA1.Set(3,true);
arr.Add (myBA1);

myBA1.SetAll(false);
myBA1.Set(0,false);
myBA1.Set(1,true);
myBA1 .Set(2,false);
myBA1.Set(3,false);
arr.Add(myBA1);

i = 0;
foreach(BitArray) x in arr)
{
Con sole.WriteLine(i ++);
DisplayBitArray(x);
}
//访问ArrayList中BitArrays的替代方法
// for(int j = 0; J&≤(arr.Count-1); j ++)
// {
// Console.WriteLine(" Array element is {0}",????);不确定
//如何在这里访问bitarray
//}

Console.ReadLine();
} //主要

public static void DisplayBitArray(IEnumerable myList)
{i / 0;
\\ n> System.Collections.IEnumerator myEnumerator =
myList.GetEnumerator();
while(myEnumerator.MoveNext())
{
Console.WriteLine(" \t [{0}]:\ t {1}",i ++,
myEnumerator.Current);
Console.WriteLine();
}
} // DisplayBitArray

public static void RemoveDuplicateBitArraysinArrayList(ArrayList
arr)
{
//逐步执行ArrayList,以便获取// BitArray元素并对每个元素进一步向下运算
//如果结果为false则将ArrayList删除,将其删除,因为它是复制

int index;

for(int i = 0; i<(arr.Count-1); i ++)
{= / index = arr.IndexOf(arr [i],i + 1);
while(index> i)
{
arr.Remove( index);
if(i<(arr.Count-1))
{
index = arr.IndexOf(arr [i],i + 1);
}

{
指数= -1;
}
}
}
} // RemoveDuplicateBitArraysinArrayList

} // Class1

} //名称空间RemoveDuplicateBitArrays




Vjeko,


我不知道我是否会循环遍历所有重复的位阵列

。由于位阵列的大小都是一样的,你可以把它放在一个比较容易的格式中。


基本上,因为它是四位,你可以将它复制到一个字节数组,其中一个元素是

。然后,你可以比较相等的字节。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" vbportal" < VB ****** @ discussions.microsoft.com>在消息中写道

新闻:FD ********************************** @ microsof t.com ...

嗨尼古拉斯 - 谢谢你的答案 - 这解决了我的第一个问题。
我仍然不确定如何解决(b)即删除重复
BitArrays
(参见下面的子例程RemoveDuplicateBitArrays)

我从网上捏了这个代码 - 这里我假设
" index = arr.IndexOf(arr [ i],
i + 1);将检查重复的BitArrays(当我读取IndexOf使用
Equals进行相等性检查时)从当前的ArrayList索引开始,然后步进一步直到ArrayList结束(重复项由
>标识)索引并删除了 - 但它不起作用 - 任何线索(我必须承认我是新的C#并且我的调试技巧远非好 - 但我们必须开始
某处;-)?

如果不是太麻烦,你还可以指出是否有任何好的信息来源,BitArrays如何适应集合中的ArrayLists <透视
- 我似乎有一个问题,理解如何解决BItArray位
/
IEnumerator如何联系(已经检查了不少书等但是没有
发现
一个令人满意的解释。

谢谢,Vjeko

Nicholas Paldino [.NET / C#MVP]"写道:
< blockquote class =post_quotes> vbportal,

问题在于y
再次添加相同的位数组。您需要做的是在再次设置
值之前创建一个新的位数组,如下所示:

ArrayList arr = new ArrayList();
BitArray myBA1 = new BitArray (4);
myBA1.SetAll(false);

myBA1.Set(0,false);
myBA1.Set(1,false);
myBA1 .Set(2,false);
myBA1.Set(3,true);
arr.Add(myBA1);

//需要在这里创建一个新的,或者你将添加相同的引用
数组列表。
myBA1 = new BitArray(4);
myBA1.SetAll(false);
//正常设置值。
myBA1.SetAll(false);
myBA1.Set(0,false);
myBA1.Set(1,true);
myBA1。设置(2,false);
myBA1.Set(3,false);
arr.Add(myBA1);

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse。 com

" vbportal" < VB ****** @ discussions.microsoft.com>在消息中写道
新闻:E2 ********************************** @ microsof t.com。 ..

>
>我想将BitArrays添加到ArrayList,然后删除任何
>重复
> - 有人可以帮我转发。我似乎有(至少;-))2
>问题/缺乏理解(见下面的测试代码):
> (a)将BitArrays添加到ArrayList然后循环通过
> ArrayList我似乎只访问最新添加的BitArray而我不是
>确切地说
>明确访问ArrayList中每个BItArray的最佳方法
>
> (b)当我尝试删除重复的BItArrays时,我似乎只是确定了
>
>当前索引为副本 (我假设IndexOf和
> Equals
>它使用的应该在这里起作用)。
>
>在此先感谢,
> Vjeko
>
>使用系统;
>使用System.Collections;
> namespace RemoveDuplicateBitArrays
> {
> class Class1
> {
> static void Main(string [] args)
> {
> ArrayList arr = new ArrayList();
> BitArray myBA1 = new BitArray(4);
> myBA1.SetAll(false);
>
> myBA1.Set(0,false);
> myBA1.Set(1,false);
> myBA1.Set(2,false);
> myBA1.Set(3,true);
> arr.Add(myBA1);
>
> myBA1.SetAll(false);
> myBA1.Set(0,false);
> myBA1.Set(1,true);
> myBA1.Set(2,false);
> myBA1.Set(3,false);
> arr.Add(myBA1);
>
>
> i = 0;
> foreach(bitArray x in arr)
> {
> Console.WriteLine(i ++);
> DisplayBitArray(x);
> }
>
> //访问ArrayList中BitArrays的另一种方法
> // for(int j = 0; j<(arr.Count-1); j ++)
> // {
> // Console.WriteLine(" Array element is {0}",????);不是
>确定
>如何
> //在这里访问bitarray的位
> //}
>
> Console.ReadLine();
> } // Main
>
>
>
> public static void DisplayBitArray(IEnumerable myList)
> {
> int i = 0;
> System.Collections.IEnumerator myEnumerator =
> myList.GetEnumerator();
> while(myEnumerator.MoveNext())
> {
> Console.WriteLine(" \t [{0}]:\t {1}",i ++,
> myEnumerator.Current);
> Console.WriteLine();
> }
> } // DisplayBitArray
>
> public static void RemoveDuplicateBitArraysinArrayList(ArrayList
> arr)
> {
> //逐步通过ArrayList,以便
> //采用BitArray元素,每个元素进一步向下XOR
> //如果结果为假,则为ArrayList,将其删除,因为它是重复的
>
> int index;
>
> for(int i = 0; i<(arr.Count-1); i ++)
> {
> index = arr.IndexOf(arr [i],i + 1);
> while(index> i)
> {
> arr.Remove(index);
> if(i<(arr.Count-1))
> {
> index = arr.IndexOf(arr [i],i + 1);
> }
>否则
> {
> index = -1;
> }
> }
> }
> } // RemoveDuplicateBitArraysinArrayList
>
> } // Class1
>
> } //名称空间RemoveDuplicateBitArrays
>




Hi,
I would like to add BitArrays to an ArrayList and then remove any duplicates
- can someone please help me forward. I seem to have (at leaset ;-) )2
problems/lack of understanding (see test code below):
(a)When adding BitArrays to the ArrayList and then looping through the
ArrayList I seem to access only the latest added BitArray and I''m not exactly
clear on best way to access each BItArray in the ArrayList

(b)When I try to remove duplicate BItArrays, I seem only to identify the
current index as being "the duplicate" (I am assuming IndexOf and the Equals
which it uses should function here).

Thanks in advance,
Vjeko

using System;
using System.Collections;
namespace RemoveDuplicateBitArrays
{
class Class1
{
static void Main(string[] args)
{
ArrayList arr = new ArrayList();
BitArray myBA1 = new BitArray( 4 );
myBA1.SetAll(false);

myBA1.Set(0,false);
myBA1.Set(1,false);
myBA1.Set(2,false);
myBA1.Set(3,true);
arr.Add(myBA1);

myBA1.SetAll(false);
myBA1.Set(0,false);
myBA1.Set(1,true);
myBA1.Set(2,false);
myBA1.Set(3,false);
arr.Add(myBA1);
i = 0;
foreach (BitArray x in arr)
{
Console.WriteLine(i++);
DisplayBitArray(x);
}

//Alternative way to access the BitArrays in the ArrayList
// for(int j=0; j<(arr.Count-1); j++)
// {
// Console.WriteLine("Array element is {0}",????); Not sure
how to
// access bits of bitarray here
// }

Console.ReadLine();
}//Main

public static void DisplayBitArray(IEnumerable myList)
{
int i = 0;
System.Collections.IEnumerator myEnumerator =
myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
Console.WriteLine( "\t[{0}]:\t{1}", i++,
myEnumerator.Current );
Console.WriteLine();
}
}//DisplayBitArray

public static void RemoveDuplicateBitArraysinArrayList(ArrayList arr)
{
//Step through ArrayList such that
//BitArray element is taken and XOR with each element further down
//the ArrayList if result is false, remove it as it is a duplicate

int index;

for(int i=0; i<(arr.Count-1); i++)
{
index=arr.IndexOf(arr[i], i+1);
while( index>i )
{
arr.Remove(index);
if( i<(arr.Count-1) )
{
index=arr.IndexOf(arr[i], i+1);
}
else
{
index=-1;
}
}
}
} //RemoveDuplicateBitArraysinArrayList

}//Class1

}//namespace RemoveDuplicateBitArrays

解决方案

vbportal,

The problem is that you are adding the same bit array over and over
again. What you need to do is create a new bit array before you set the
values again, like so:

ArrayList arr = new ArrayList();
BitArray myBA1 = new BitArray( 4 );
myBA1.SetAll(false);

myBA1.Set(0,false);
myBA1.Set(1,false);
myBA1.Set(2,false);
myBA1.Set(3,true);
arr.Add(myBA1);

// Need to create a new one here, or you will add the same reference to the
array list.
myBA1 = new BitArray(4);
myBA1.SetAll(false);

// Set values normally.
myBA1.SetAll(false);
myBA1.Set(0,false);
myBA1.Set(1,true);
myBA1.Set(2,false);
myBA1.Set(3,false);
arr.Add(myBA1);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"vbportal" <vb******@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...

Hi,
I would like to add BitArrays to an ArrayList and then remove any
duplicates
- can someone please help me forward. I seem to have (at leaset ;-) )2
problems/lack of understanding (see test code below):
(a)When adding BitArrays to the ArrayList and then looping through the
ArrayList I seem to access only the latest added BitArray and I''m not
exactly
clear on best way to access each BItArray in the ArrayList

(b)When I try to remove duplicate BItArrays, I seem only to identify the
current index as being "the duplicate" (I am assuming IndexOf and the
Equals
which it uses should function here).

Thanks in advance,
Vjeko

using System;
using System.Collections;
namespace RemoveDuplicateBitArrays
{
class Class1
{
static void Main(string[] args)
{
ArrayList arr = new ArrayList();
BitArray myBA1 = new BitArray( 4 );
myBA1.SetAll(false);

myBA1.Set(0,false);
myBA1.Set(1,false);
myBA1.Set(2,false);
myBA1.Set(3,true);
arr.Add(myBA1);

myBA1.SetAll(false);
myBA1.Set(0,false);
myBA1.Set(1,true);
myBA1.Set(2,false);
myBA1.Set(3,false);
arr.Add(myBA1);
i = 0;
foreach (BitArray x in arr)
{
Console.WriteLine(i++);
DisplayBitArray(x);
}

//Alternative way to access the BitArrays in the ArrayList
// for(int j=0; j<(arr.Count-1); j++)
// {
// Console.WriteLine("Array element is {0}",????); Not sure
how to
// access bits of bitarray here
// }

Console.ReadLine();
}//Main

public static void DisplayBitArray(IEnumerable myList)
{
int i = 0;
System.Collections.IEnumerator myEnumerator =
myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
Console.WriteLine( "\t[{0}]:\t{1}", i++,
myEnumerator.Current );
Console.WriteLine();
}
}//DisplayBitArray

public static void RemoveDuplicateBitArraysinArrayList(ArrayList
arr)
{
//Step through ArrayList such that
//BitArray element is taken and XOR with each element further down
//the ArrayList if result is false, remove it as it is a duplicate

int index;

for(int i=0; i<(arr.Count-1); i++)
{
index=arr.IndexOf(arr[i], i+1);
while( index>i )
{
arr.Remove(index);
if( i<(arr.Count-1) )
{
index=arr.IndexOf(arr[i], i+1);
}
else
{
index=-1;
}
}
}
} //RemoveDuplicateBitArraysinArrayList

}//Class1

}//namespace RemoveDuplicateBitArrays



Hi Nicholas - thanks for the answer - this sorted out my first problem.
I''m still not sure how to solve (b) i.e. for the removal of duplicate
BitArrays
(see subroutine RemoveDuplicateBitArrays below)

I pinched this code from the net - here I assume "index=arr.IndexOf(arr[i],
i+1);" will check for duplicate BitArrays (where as I read IndexOf uses
Equals for the equality check)starting at present ArrayList index and
stepping one ahead till end of ArrayList (duplicates are identified by
"index" and removed - but it doesn''t work - any clue (I must admit I''m quite
new to C# and my debugging skills are far from good - but we have to start
somewhere ;-) ?

If it''s not too much bother could you also indicate if there is any good
source of info how BitArrays fit in ArrayLists from a Collections perspective
- I seem to have a problem understanding how to address the BItArray bits /
how IEnumerator ties in (have checked quite a few books etc but haven''t found
a satisfactory explanation.

Thanks,Vjeko

"Nicholas Paldino [.NET/C# MVP]" wrote:

vbportal,

The problem is that you are adding the same bit array over and over
again. What you need to do is create a new bit array before you set the
values again, like so:

ArrayList arr = new ArrayList();
BitArray myBA1 = new BitArray( 4 );
myBA1.SetAll(false);

myBA1.Set(0,false);
myBA1.Set(1,false);
myBA1.Set(2,false);
myBA1.Set(3,true);
arr.Add(myBA1);

// Need to create a new one here, or you will add the same reference to the
array list.
myBA1 = new BitArray(4);
myBA1.SetAll(false);

// Set values normally.
myBA1.SetAll(false);
myBA1.Set(0,false);
myBA1.Set(1,true);
myBA1.Set(2,false);
myBA1.Set(3,false);
arr.Add(myBA1);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"vbportal" <vb******@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...

Hi,
I would like to add BitArrays to an ArrayList and then remove any
duplicates
- can someone please help me forward. I seem to have (at leaset ;-) )2
problems/lack of understanding (see test code below):
(a)When adding BitArrays to the ArrayList and then looping through the
ArrayList I seem to access only the latest added BitArray and I''m not
exactly
clear on best way to access each BItArray in the ArrayList

(b)When I try to remove duplicate BItArrays, I seem only to identify the
current index as being "the duplicate" (I am assuming IndexOf and the
Equals
which it uses should function here).

Thanks in advance,
Vjeko

using System;
using System.Collections;
namespace RemoveDuplicateBitArrays
{
class Class1
{
static void Main(string[] args)
{
ArrayList arr = new ArrayList();
BitArray myBA1 = new BitArray( 4 );
myBA1.SetAll(false);

myBA1.Set(0,false);
myBA1.Set(1,false);
myBA1.Set(2,false);
myBA1.Set(3,true);
arr.Add(myBA1);

myBA1.SetAll(false);
myBA1.Set(0,false);
myBA1.Set(1,true);
myBA1.Set(2,false);
myBA1.Set(3,false);
arr.Add(myBA1);
i = 0;
foreach (BitArray x in arr)
{
Console.WriteLine(i++);
DisplayBitArray(x);
}

//Alternative way to access the BitArrays in the ArrayList
// for(int j=0; j<(arr.Count-1); j++)
// {
// Console.WriteLine("Array element is {0}",????); Not sure
how to
// access bits of bitarray here
// }

Console.ReadLine();
}//Main

public static void DisplayBitArray(IEnumerable myList)
{
int i = 0;
System.Collections.IEnumerator myEnumerator =
myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
Console.WriteLine( "\t[{0}]:\t{1}", i++,
myEnumerator.Current );
Console.WriteLine();
}
}//DisplayBitArray

public static void RemoveDuplicateBitArraysinArrayList(ArrayList
arr)
{
//Step through ArrayList such that
//BitArray element is taken and XOR with each element further down
//the ArrayList if result is false, remove it as it is a duplicate

int index;

for(int i=0; i<(arr.Count-1); i++)
{
index=arr.IndexOf(arr[i], i+1);
while( index>i )
{
arr.Remove(index);
if( i<(arr.Count-1) )
{
index=arr.IndexOf(arr[i], i+1);
}
else
{
index=-1;
}
}
}
} //RemoveDuplicateBitArraysinArrayList

}//Class1

}//namespace RemoveDuplicateBitArrays




Vjeko,

I don''t know if I would cycle through all of the duplicate bit arrays
the way you are. Since the sizes of the bit arrays are all the same, you
can just place it in a format that is easier to compare.

Basically, since it is four bits, you can copy that to a byte array with
one element. Then, you can just compare the bytes for equality.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"vbportal" <vb******@discussions.microsoft.com> wrote in message
news:FD**********************************@microsof t.com...

Hi Nicholas - thanks for the answer - this sorted out my first problem.
I''m still not sure how to solve (b) i.e. for the removal of duplicate
BitArrays
(see subroutine RemoveDuplicateBitArrays below)

I pinched this code from the net - here I assume
"index=arr.IndexOf(arr[i],
i+1);" will check for duplicate BitArrays (where as I read IndexOf uses
Equals for the equality check)starting at present ArrayList index and
stepping one ahead till end of ArrayList (duplicates are identified by
"index" and removed - but it doesn''t work - any clue (I must admit I''m
quite
new to C# and my debugging skills are far from good - but we have to start
somewhere ;-) ?

If it''s not too much bother could you also indicate if there is any good
source of info how BitArrays fit in ArrayLists from a Collections
perspective
- I seem to have a problem understanding how to address the BItArray bits
/
how IEnumerator ties in (have checked quite a few books etc but haven''t
found
a satisfactory explanation.

Thanks,Vjeko

"Nicholas Paldino [.NET/C# MVP]" wrote:

vbportal,

The problem is that you are adding the same bit array over and over
again. What you need to do is create a new bit array before you set the
values again, like so:

ArrayList arr = new ArrayList();
BitArray myBA1 = new BitArray( 4 );
myBA1.SetAll(false);

myBA1.Set(0,false);
myBA1.Set(1,false);
myBA1.Set(2,false);
myBA1.Set(3,true);
arr.Add(myBA1);

// Need to create a new one here, or you will add the same reference to
the
array list.
myBA1 = new BitArray(4);
myBA1.SetAll(false);

// Set values normally.
myBA1.SetAll(false);
myBA1.Set(0,false);
myBA1.Set(1,true);
myBA1.Set(2,false);
myBA1.Set(3,false);
arr.Add(myBA1);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"vbportal" <vb******@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...

> Hi,
> I would like to add BitArrays to an ArrayList and then remove any
> duplicates
> - can someone please help me forward. I seem to have (at leaset ;-) )2
> problems/lack of understanding (see test code below):
> (a)When adding BitArrays to the ArrayList and then looping through the
> ArrayList I seem to access only the latest added BitArray and I''m not
> exactly
> clear on best way to access each BItArray in the ArrayList
>
> (b)When I try to remove duplicate BItArrays, I seem only to identify
> the
> current index as being "the duplicate" (I am assuming IndexOf and the
> Equals
> which it uses should function here).
>
> Thanks in advance,
> Vjeko
>
> using System;
> using System.Collections;
> namespace RemoveDuplicateBitArrays
> {
> class Class1
> {
> static void Main(string[] args)
> {
> ArrayList arr = new ArrayList();
> BitArray myBA1 = new BitArray( 4 );
> myBA1.SetAll(false);
>
> myBA1.Set(0,false);
> myBA1.Set(1,false);
> myBA1.Set(2,false);
> myBA1.Set(3,true);
> arr.Add(myBA1);
>
> myBA1.SetAll(false);
> myBA1.Set(0,false);
> myBA1.Set(1,true);
> myBA1.Set(2,false);
> myBA1.Set(3,false);
> arr.Add(myBA1);
>
>
> i = 0;
> foreach (BitArray x in arr)
> {
> Console.WriteLine(i++);
> DisplayBitArray(x);
> }
>
> //Alternative way to access the BitArrays in the ArrayList
> // for(int j=0; j<(arr.Count-1); j++)
> // {
> // Console.WriteLine("Array element is {0}",????); Not
> sure
> how to
> // access bits of bitarray here
> // }
>
> Console.ReadLine();
> }//Main
>
>
>
> public static void DisplayBitArray(IEnumerable myList)
> {
> int i = 0;
> System.Collections.IEnumerator myEnumerator =
> myList.GetEnumerator();
> while ( myEnumerator.MoveNext() )
> {
> Console.WriteLine( "\t[{0}]:\t{1}", i++,
> myEnumerator.Current );
> Console.WriteLine();
> }
> }//DisplayBitArray
>
> public static void RemoveDuplicateBitArraysinArrayList(ArrayList
> arr)
> {
> //Step through ArrayList such that
> //BitArray element is taken and XOR with each element further down
> //the ArrayList if result is false, remove it as it is a duplicate
>
> int index;
>
> for(int i=0; i<(arr.Count-1); i++)
> {
> index=arr.IndexOf(arr[i], i+1);
> while( index>i )
> {
> arr.Remove(index);
> if( i<(arr.Count-1) )
> {
> index=arr.IndexOf(arr[i], i+1);
> }
> else
> {
> index=-1;
> }
> }
> }
> } //RemoveDuplicateBitArraysinArrayList
>
> }//Class1
>
> }//namespace RemoveDuplicateBitArrays
>




这篇关于删除ArrayList中的重复BitArrays的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆