`
索菲亚.  ぅ
  • 浏览: 16389 次
  • 性别: Icon_minigender_2
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

4.24学习笔记

    博客分类:
  • NET
阅读更多

C#中
数组长度表示用.length;
arraylist集合中对象个数表示用.count;

 

c#数组的声明方式

int[] intArray;
int[] intArray = {99,34,56,12,1};
int[] intArray = new int[5];
int[] intArray = new int[varsize];
int[] intArray = new int[5]{99,34,56,12,1};

 

从旧数组复制到新数组,2表示新数组开始位置

oldArray.CopyTo(newArray, 2);

 循环迭代出Hashtable中对象
错误:

foreach(Person  p in peopleHashtable) // Will compile but not run!
{
    Response.Write(p.FullName + "<BR/>");
} 

 正确:

foreach (DictionaryEntry de in peopleHashtable)
{
    Response.Write(de.Key.ToString() + ":" + ((Person)de.Value).FullName + 
       "<BR/>");
}

 

 

SortedList

1创建空的SortedList
2手动迭代Hashtable的DictionaryEntry(Hashtable是无须存放的)
3把它们添加到SortedList(添加过程中自动排序)
实例:
SortedList peopleSortedList = new SortedList(peopleHashtable);
foreach (Person p in peopleSortedList.Values)
{
    Response.Write(p.FullName + "<BR/>");
}
Response.Write("Index of Srinivasa: " + peopleSortedList.IndexOfKey("ss"));

 

Queues适合于按照对象进入的顺序来存储它们(先进先出),

而Stack是一个先进后出结构。

但要取的话必须删一个取一个

 

BitArray
管理一个布尔值数组或者集合使用

 


强类型化的集合如PersonList类
注意:强制类型转换隐藏在了属性访问器中,属性的返回值是强类型化的。(这里this是C#属性访问器的关键字)

 

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Collections;

/// <summary>
///PersonList 的摘要说明
/// </summary>
public class PersonList:System.Collections.IEnumerable
{
 public PersonList()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
 }
    private ArrayList innerList = new ArrayList();

    public void Add(Person aPerson)
    {
        innerList.Add(aPerson);
    }

    public void Remove(Person aPerson)
    {
        innerList.Remove(aPerson);
    }

    public int Count
    {
        get
        {
            return innerList.Count;
        }
    }

    //Get/Set element at given index
    public Person this[int index]
    {
        get 
        {
            return (Person)innerList[index];
        }
        set
        {
            innerList[index] = value;
        }
    }

    public IEnumerator GetEnumerator()
    {
        return innerList.GetEnumerator();
    }
}

 

 泛型的Hashtable 和Hashtable区别,读取的时候不需要强制类型转化

 

LinkedList<T>是一个强类型化的双链接表,该链接表中的每个节点都指向前一个节点和后一个节点

 

 问题:谁能帮我讲解下BinarySearch方法?小女谢谢了。。

分享到:
评论
4 楼 尔今尔后 2012-02-03  
小女子 姿色不错呀~·~~
3 楼 kimmking 2009-04-29  
<div class="quote_title">索菲亚.  ぅ 写道</div><div class="quote_div"><div class="quote_title">kimmking 写道</div>
<div class="quote_div">
<pre name="code" class="c#"> ArrayList的binary search:
</pre>
<pre name="code" class="c#">// Searches a section of the list for a given element using a binary search
        // algorithm. Elements of the list are compared to the search value using
        // the given IComparer interface. If comparer is null, elements of
        // the list are compared to the search value using the IComparable
        // interface, which in that case must be implemented by all elements of the
        // list and the given search value. This method assumes that the given
        // section of the list is already sorted; if this is not the case, the
        // result will be incorrect.
        //
        // The method returns the index of the given value in the list. If the
        // list does not contain the given value, the method returns a negative
        // integer. The bitwise complement operator (~) can be applied to a
        // negative result to produce the index of the first element (if any) that
        // is larger than the given search value. This is also the index at which
        // the search value should be inserted into the list in order for the list
        // to remain sorted.
        //
        // The method uses the Array.BinarySearch method to perform the
        // search.
        //
        public virtual int BinarySearch(int index, int count, Object value, IComparer comparer) {
            if (index &lt; 0 || count &lt; 0)
                throw new ArgumentOutOfRangeException((index&lt;0 ? "index" : "count"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            if (_size - index &lt; count)
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
   
            return Array.BinarySearch((Array)_items, index, count, value, comparer);
        }
</pre>
<p> </p>
<p> </p>
<p>Array的:</p>
<pre name="code" class="c#">[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
        public static int BinarySearch(Array array, int index, int length, Object value, IComparer comparer) {
            if (array==null)
                throw new ArgumentNullException("array");
            int lb = array.GetLowerBound(0);
            if (index &lt; lb || length &lt; 0)
                throw new ArgumentOutOfRangeException((index&lt;lb ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            if (array.Length - (index - lb) &lt; length)
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
            if (array.Rank != 1)
                throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
           
            if (comparer == null) comparer = Comparer.Default;
            if (comparer == Comparer.Default) {
                int retval;
                bool r = TrySZBinarySearch(array, index, length, value, out retval);
                if (r)
                    return retval;
            }

            int lo = index;
            int hi = index + length - 1;  
            Object[] objArray = array as Object[];
            if(objArray != null) {
                while (lo &lt;= hi) {
                    // i might overflow if lo and hi are both large positive numbers.
                    int i = GetMedian(lo, hi);

                    int c;
                    try {
                        c = comparer.Compare(objArray[i], value);
                    }
                    catch (Exception e) {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
                    }
                    catch {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
                    }
                    if (c == 0) return i;
                    if (c &lt; 0) {
                        lo = i + 1;
                    }
                    else {
                        hi = i - 1;
                    }
                }
            }
            else {
                while (lo &lt;= hi) {
                    int i = GetMedian(lo, hi);                   

                    int c;
                    try {
                        c = comparer.Compare(array.GetValue(i), value);
                    }
                    catch (Exception e) {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
                    }
                    catch {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
                    }
                    if (c == 0) return i;
                    if (c &lt; 0) {
                        lo = i + 1;
                    }
                    else {
                        hi = i - 1;
                    }
                }
            }
            return ~lo;
        }
</pre>
<p> </p>
<p>总的来说,就是二分法来查找元素,如果使用此方法前没有排序,或是查找的比较方式和排序的比较方式不一样的话,结果可能找不到。</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<p><br>谢谢昂。····多用在什么时候呢?</p></div><br/>用在已经排序过了的时候,特别是数据量比较大的时候,可以提高查找效率。

一般不用,也许你一辈子用不着。


----mm加我qq  3694826
2 楼 索菲亚.  ぅ 2009-04-29  
<div class="quote_title">kimmking 写道</div>
<div class="quote_div">
<pre name="code" class="c#"> ArrayList的binary search:
</pre>
<pre name="code" class="c#">// Searches a section of the list for a given element using a binary search
        // algorithm. Elements of the list are compared to the search value using
        // the given IComparer interface. If comparer is null, elements of
        // the list are compared to the search value using the IComparable
        // interface, which in that case must be implemented by all elements of the
        // list and the given search value. This method assumes that the given
        // section of the list is already sorted; if this is not the case, the
        // result will be incorrect.
        //
        // The method returns the index of the given value in the list. If the
        // list does not contain the given value, the method returns a negative
        // integer. The bitwise complement operator (~) can be applied to a
        // negative result to produce the index of the first element (if any) that
        // is larger than the given search value. This is also the index at which
        // the search value should be inserted into the list in order for the list
        // to remain sorted.
        //
        // The method uses the Array.BinarySearch method to perform the
        // search.
        //
        public virtual int BinarySearch(int index, int count, Object value, IComparer comparer) {
            if (index &lt; 0 || count &lt; 0)
                throw new ArgumentOutOfRangeException((index&lt;0 ? "index" : "count"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            if (_size - index &lt; count)
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
   
            return Array.BinarySearch((Array)_items, index, count, value, comparer);
        }
</pre>
<p> </p>
<p> </p>
<p>Array的:</p>
<pre name="code" class="c#">[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
        public static int BinarySearch(Array array, int index, int length, Object value, IComparer comparer) {
            if (array==null)
                throw new ArgumentNullException("array");
            int lb = array.GetLowerBound(0);
            if (index &lt; lb || length &lt; 0)
                throw new ArgumentOutOfRangeException((index&lt;lb ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            if (array.Length - (index - lb) &lt; length)
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
            if (array.Rank != 1)
                throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
           
            if (comparer == null) comparer = Comparer.Default;
            if (comparer == Comparer.Default) {
                int retval;
                bool r = TrySZBinarySearch(array, index, length, value, out retval);
                if (r)
                    return retval;
            }

            int lo = index;
            int hi = index + length - 1;  
            Object[] objArray = array as Object[];
            if(objArray != null) {
                while (lo &lt;= hi) {
                    // i might overflow if lo and hi are both large positive numbers.
                    int i = GetMedian(lo, hi);

                    int c;
                    try {
                        c = comparer.Compare(objArray[i], value);
                    }
                    catch (Exception e) {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
                    }
                    catch {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
                    }
                    if (c == 0) return i;
                    if (c &lt; 0) {
                        lo = i + 1;
                    }
                    else {
                        hi = i - 1;
                    }
                }
            }
            else {
                while (lo &lt;= hi) {
                    int i = GetMedian(lo, hi);                   

                    int c;
                    try {
                        c = comparer.Compare(array.GetValue(i), value);
                    }
                    catch (Exception e) {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
                    }
                    catch {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
                    }
                    if (c == 0) return i;
                    if (c &lt; 0) {
                        lo = i + 1;
                    }
                    else {
                        hi = i - 1;
                    }
                }
            }
            return ~lo;
        }
</pre>
<p> </p>
<p>总的来说,就是二分法来查找元素,如果使用此方法前没有排序,或是查找的比较方式和排序的比较方式不一样的话,结果可能找不到。</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<p><br>谢谢昂。····多用在什么时候呢?</p>
1 楼 kimmking 2009-04-28  
<pre name="code" class="c#"> ArrayList的binary search:
</pre>
<pre name="code" class="c#">// Searches a section of the list for a given element using a binary search
        // algorithm. Elements of the list are compared to the search value using
        // the given IComparer interface. If comparer is null, elements of
        // the list are compared to the search value using the IComparable
        // interface, which in that case must be implemented by all elements of the
        // list and the given search value. This method assumes that the given
        // section of the list is already sorted; if this is not the case, the
        // result will be incorrect.
        //
        // The method returns the index of the given value in the list. If the
        // list does not contain the given value, the method returns a negative
        // integer. The bitwise complement operator (~) can be applied to a
        // negative result to produce the index of the first element (if any) that
        // is larger than the given search value. This is also the index at which
        // the search value should be inserted into the list in order for the list
        // to remain sorted.
        //
        // The method uses the Array.BinarySearch method to perform the
        // search.
        //
        public virtual int BinarySearch(int index, int count, Object value, IComparer comparer) {
            if (index &lt; 0 || count &lt; 0)
                throw new ArgumentOutOfRangeException((index&lt;0 ? "index" : "count"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            if (_size - index &lt; count)
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
   
            return Array.BinarySearch((Array)_items, index, count, value, comparer);
        }
</pre>
<p> </p>
<p> </p>
<p>Array的:</p>
<pre name="code" class="c#">[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
        public static int BinarySearch(Array array, int index, int length, Object value, IComparer comparer) {
            if (array==null)
                throw new ArgumentNullException("array");
            int lb = array.GetLowerBound(0);
            if (index &lt; lb || length &lt; 0)
                throw new ArgumentOutOfRangeException((index&lt;lb ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            if (array.Length - (index - lb) &lt; length)
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
            if (array.Rank != 1)
                throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
           
            if (comparer == null) comparer = Comparer.Default;
            if (comparer == Comparer.Default) {
                int retval;
                bool r = TrySZBinarySearch(array, index, length, value, out retval);
                if (r)
                    return retval;
            }

            int lo = index;
            int hi = index + length - 1;  
            Object[] objArray = array as Object[];
            if(objArray != null) {
                while (lo &lt;= hi) {
                    // i might overflow if lo and hi are both large positive numbers.
                    int i = GetMedian(lo, hi);

                    int c;
                    try {
                        c = comparer.Compare(objArray[i], value);
                    }
                    catch (Exception e) {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
                    }
                    catch {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
                    }
                    if (c == 0) return i;
                    if (c &lt; 0) {
                        lo = i + 1;
                    }
                    else {
                        hi = i - 1;
                    }
                }
            }
            else {
                while (lo &lt;= hi) {
                    int i = GetMedian(lo, hi);                   

                    int c;
                    try {
                        c = comparer.Compare(array.GetValue(i), value);
                    }
                    catch (Exception e) {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e);
                    }
                    catch {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
                    }
                    if (c == 0) return i;
                    if (c &lt; 0) {
                        lo = i + 1;
                    }
                    else {
                        hi = i - 1;
                    }
                }
            }
            return ~lo;
        }
</pre>
<p> </p>
<p>总的来说,就是二分法来查找元素,如果使用此方法前没有排序,或是查找的比较方式和排序的比较方式不一样的话,结果可能找不到。</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>

相关推荐

    4.24师德师风政治学习笔记.pdf

    教师自身需要关注人文关怀,持续学习,提升人格魅力,实现自我完善。学校应营造良好的师德氛围,提供支持和引导。社会则需理解和尊重教师,为教师的成长提供良好的环境,共同促进教育事业的健康发展。通过这样的共同...

    Docker Desktop Installer 4.24.2

    Docker Desktop Installer 4.24.2

    arcgis api for javascript v4.24

    《ArcGIS API for JavaScript v4.24:深入解析与应用》 ArcGIS API for JavaScript是Esri公司推出的一款强大的地图开发工具,主要用于构建交互式的Web GIS应用。v4.24版本作为其一个重要的更新,引入了许多新特性和...

    ashly PRO4.24C

    ashly 矩阵及4.24C 处理器软件

    nrf conncet 4.24.3 apk

    《nrf Connect 4.24.3 APK:蓝牙开发的得力助手》 在现代物联网(IoT)领域,蓝牙技术扮演着至关重要的角色,它使得设备间的无线通信变得更加便捷。nrf Connect 4.24.3 APK 是一个专门为开发者设计的蓝牙开发辅助...

    JINK_V8_4.24f固件

    JINK_V8_4.24f固件

    STM32CubeMX 4.24.0

    版本4.24.0是该软件的一个特定更新,提供了对一系列STM32芯片的支持以及最新的功能改进和修复。STM32CubeMX基于图形用户界面,使得开发者能够方便地配置系统时钟、GPIO引脚、串行通信接口、ADC、DAC、定时器等,大大...

    CXONE 4.24

    【CXONE 4.24】是欧姆龙(Omron)公司推出的一款先进的集成软件,主要用于自动化控制和工业生产领域的应用。...通过持续学习和掌握这款软件,可以提高工程项目的质量和效率,推动工厂自动化水平的提升。

    UE4.24.3版本源码

    UE4.24.3版本源码,和Github发布版本相同,下载时间2022.7.18。

    HBuilderX.4.24.2024072208.zip

    H是HTML的首字母,Builder是构造者,X是HBuilder的下一代版本。我们也简称HX。 HX是轻如编辑器、强如IDE的合体版本

    联想工程师专用小工具 Office卸载工具V4.24.1

    联想工程师专用小工具 Office卸载工具V4.24.1联想工程师专用小工具 Office卸载工具V4.24.1联想工程师专用小工具 Office卸载工具V4.24.1联想工程师专用小工具 Office卸载工具V4.24.1联想工程师专用小工具 Office卸载...

    P2P终结者 4.24

    P2P终结者 4.24,可以控制网速,很实用。

    TagInfo 4.24.6

    标题“TagInfo 4.24.6”指的是一个软件版本,这可能是一个用于处理NFC(近场通信)技术的工具,如信息读取、分析或管理的应用。NFC是一种无线通信技术,允许设备在近距离内交换数据,通常用于移动支付、门禁系统、...

    p2p终结者4.24版本

    这个p2p终结者4.24版本,此版本为最新版

    jlink-4.24驱动

    驱动软件!jlink的驱动!用于j-link的使用!

    CX-ONE4.24有用的序列号

    在探讨“CX-ONE4.24有用的序列号”这一主题时,首先需要明确几个关键概念:CX-ONE是一款由欧姆龙(OMRON)公司开发的编程软件,主要用于为欧姆龙PLC(可编程逻辑控制器)进行编程。而序列号则是用于验证软件合法性的...

    libev-4.24.tar.gz

    版本4.24是libev的一个稳定版本,提供了许多改进和修复,确保了在各种操作系统上的可靠性和效率。 **一、libev的基本概念** 1. **事件库**:libev是一个跨平台的事件循环库,它允许程序员以一种统一的方式处理各种...

    UnrealEngine-4.24.1-release源码

    总而言之,Unreal Engine 4.24.1的源码是一个巨大的知识宝库,包含了游戏开发的各个方面,无论是对于初学者还是经验丰富的开发者,都有极高的学习和研究价值。通过深入研究源码,开发者不仅可以提升自己的编程技巧,...

    libev-4.24 最新代码

    `libev-4.24`是一个强大的事件库,对于需要高效处理异步事件的C/C++开发者来说,是一个重要的工具。它的高性能、跨平台特性和简洁API使其在系统编程和网络应用中备受青睐。通过理解和掌握`libev`的使用,开发者可以...

    Docker-desktop 4.24.1.0 解决报错19044

    如果Windows安装docker的时候报错19044 or above,就使用这个版本

Global site tag (gtag.js) - Google Analytics