确定一项出现在List中的次数
来源:互联网 发布:昆仑墟进阶数据 编辑:程序博客网 时间:2024/05/22 14:12
在List<T>包含了一些查找某一项的方法(如:Contains和BinarySearch),但是,这些方法无法实现查找重复项,所以利用以下2种方法(CountAll和BinarySearchCountALL)实现 返回一个特定对象出现在有序和无序List<T>中的次数。
以下测试代码:
CountAll方法使用For循环线性查找,而BinarySearchCountAll方法使用了二分查找。
CountAll方法接受一个类型为Object的搜索值,这个方法再统计该搜索值出现在ListEx<T>类中的次数,此方法有序或者无序都可以使用,如果ListEx<T>为有序的(ListEx<T>通过调用Sort方法已经排序),可以用BinarySearchCountAll方法来提高搜索效率。与迭代处理这个ListEx<T>相比,这个方法的速度要快,随着ListEx<T>规模的增长,效率提升。
using System;
using System.Collections;
using System.Collections.Generic;
public class ListEx<T> : List<T>
{
// Count the number of times an item appears in this
// unsorted or sorted List<T>
public int CountAll(T searchValue)
{
int foundCounter = 0;
for (int index = 0; index < this.Count; index++)
{
if (this[index].Equals(searchValue))
{
foundCounter++;
}
}
return (foundCounter);
}
// Count the number of times an item appears in this sorted List<T>.
public int BinarySearchCountAll(T searchValue)
{
// Search for first item.
int center = this.BinarySearch(searchValue);
int left = center;
while (left >0 && this[left-1].Equals(searchValue))
{
left -= 1;
}
int right = center;
while (right < (this.Count - 1) && this[right+1].Equals(searchValue))
{
right += 1;
}
return (right -left) + 1;
}
}
using System.Collections;
using System.Collections.Generic;
public class ListEx<T> : List<T>
{
// Count the number of times an item appears in this
// unsorted or sorted List<T>
public int CountAll(T searchValue)
{
int foundCounter = 0;
for (int index = 0; index < this.Count; index++)
{
if (this[index].Equals(searchValue))
{
foundCounter++;
}
}
return (foundCounter);
}
// Count the number of times an item appears in this sorted List<T>.
public int BinarySearchCountAll(T searchValue)
{
// Search for first item.
int center = this.BinarySearch(searchValue);
int left = center;
while (left >0 && this[left-1].Equals(searchValue))
{
left -= 1;
}
int right = center;
while (right < (this.Count - 1) && this[right+1].Equals(searchValue))
{
right += 1;
}
return (right -left) + 1;
}
}
以下测试代码:
class Test
{
static void Main()
{
ListEx<int> arrayExt = new ListEx<int>();
arrayExt.Add(-2);
arrayExt.Add(-2);
arrayExt.Add(-1);
arrayExt.Add(-1);
arrayExt.Add(1);
arrayExt.Add(2);
arrayExt.Add(2);
arrayExt.Add(2);
arrayExt.Add(2);
arrayExt.Add(3);
arrayExt.Add(100);
arrayExt.Add(4);
arrayExt.Add(5);
Console.WriteLine("--CONTAINS TOTAL--");
int count = arrayExt.CountAll(2);
Console.WriteLine("Count2: " + count);
count = arrayExt.CountAll(3);
Console.WriteLine("Count3: " + count);
count = arrayExt.CountAll(1);
Console.WriteLine("Count1: " + count);
Console.WriteLine(" --BINARY SEARCH COUNT ALL--");
arrayExt.Sort();
count = arrayExt.BinarySearchCountAll(2);
Console.WriteLine("Count2: " + count);
count = arrayExt.BinarySearchCountAll(3);
Console.WriteLine("Count3: " + count);
count = arrayExt.BinarySearchCountAll(1);
Console.WriteLine("Count1: " + count);
}
}
{
static void Main()
{
ListEx<int> arrayExt = new ListEx<int>();
arrayExt.Add(-2);
arrayExt.Add(-2);
arrayExt.Add(-1);
arrayExt.Add(-1);
arrayExt.Add(1);
arrayExt.Add(2);
arrayExt.Add(2);
arrayExt.Add(2);
arrayExt.Add(2);
arrayExt.Add(3);
arrayExt.Add(100);
arrayExt.Add(4);
arrayExt.Add(5);
Console.WriteLine("--CONTAINS TOTAL--");
int count = arrayExt.CountAll(2);
Console.WriteLine("Count2: " + count);
count = arrayExt.CountAll(3);
Console.WriteLine("Count3: " + count);
count = arrayExt.CountAll(1);
Console.WriteLine("Count1: " + count);
Console.WriteLine(" --BINARY SEARCH COUNT ALL--");
arrayExt.Sort();
count = arrayExt.BinarySearchCountAll(2);
Console.WriteLine("Count2: " + count);
count = arrayExt.BinarySearchCountAll(3);
Console.WriteLine("Count3: " + count);
count = arrayExt.BinarySearchCountAll(1);
Console.WriteLine("Count1: " + count);
}
}
CountAll方法使用For循环线性查找,而BinarySearchCountAll方法使用了二分查找。
CountAll方法接受一个类型为Object的搜索值,这个方法再统计该搜索值出现在ListEx<T>类中的次数,此方法有序或者无序都可以使用,如果ListEx<T>为有序的(ListEx<T>通过调用Sort方法已经排序),可以用BinarySearchCountAll方法来提高搜索效率。与迭代处理这个ListEx<T>相比,这个方法的速度要快,随着ListEx<T>规模的增长,效率提升。
- 确定一项出现在List中的次数
- java 求一个list集合中出现次数最多的一项,和此项的出现次数
- java 求一个list集合中出现次数最多的一项,和此项的出现次数
- 计算Java List中的重复项出现次数【转】
- 计算Java List中的重复项出现次数
- java统计List中的元素重复出现的次数
- 计算Java List中的重复项出现次数【转】
- 统计List中的元素重复出现的次数
- 计算Java List中的重复项出现次数
- 计算Java List中的重复项出现次数
- 计算Java List中的重复项出现次数
- 计算Java List中的重复项出现次数
- 计算Java List中的重复项出现次数【转】
- 查找指定字符串在某个字符串中的出现次数
- list列表去除重复项以及查询某个字符串在list的出现次数
- 关于List每一项改变状态出现混乱的解决办法
- Python将list中的每一项的首字母大写
- list集合统计元素出现的次数
- 我的程序作品(调查评测系统)
- 我的程序作品(场地管理系统)
- PPP协议解析
- 尝试在 Windows Server 2003 或 Windows XP 中安装大型 Windows Installer 程序包或大型 Windows Installer 修补程序包时出现错误消息:“Error 1718. File was rejecte
- = 网络基础总汇 =
- 确定一项出现在List中的次数
- TNS-12541: TNS: 无监听程序
- asp.net在ie7中使用FileUpload上传前预览图片
- RC4加密算法在C++中的实现(转)
- FTP命令大全
- socket 编程
- FTP命令行使用精萃
- cannot be resolved to a type的解决方案
- 一个短信系统的服务器端设计