zoukankan      html  css  js  c++  java
  • 简单数据结构(五)串和数组

         在应用程序中使用最频繁的类型是字符串。字符串简称串,是一种特殊的线性表,其特殊性在于串中的数据元素是一个个的字符。字符串在计算机的许多方面应用很广。如在汇编和高级语言的编译程序中,源程序和目标程序都是字符串数据。在事务处理程序中,顾客的信息如姓名、地址等及货物的名称、产地和规格等,都被作为字符串来处理。另外,字符串还具有自身的一些特性。因此,把字符串作为一种数据结构来研究。

    串的基本概念

    串(String)由 n(n≥0)字符组成的有限序列。一般记为:
    S=”c1c2…cn” (n≥0)
    其中, S是串名,双引号作为串的定界符,用双引号引起来的字符序列是串值。 ci( 1≤i≤n)可以是字母、数字或其它字符, n为串的长度,当n=0 时,称为空串(Empty String)。
    串中任意个连续的字符组成的子序列称为该串的子串(Substring)。包含子串的串相应地称为主串。子串的第一个字符在主串中的位置叫子串的位置。如串s1”abcdefg”,它的长度是 7,串s2”cdef”的长度是 4, s2是s1的子串, s2的位置是 3。
    如果两个串的长度相等并且对应位置的字符都相等,则称这两个串相等。而在 C#中,比较两个串是否相等还要看串的语言文化等信息。

    串的存储

    由于串中的字符都是连续存储的,而在 C#中串具有恒定不变的特性,即字符串一经创建,就不能将其变长、变短或者改变其中任何的字符。所以,这里不讨论串的链式存储,也不用接口来表示串的操作。

    串的代码实现

    把串看作是一个类,类名为 StringDS。取名为 StringDS 是为了和 C#自身的字符串类 String 相区别。类StringDS 只有一个字段,即存放串中字符序列的数组 data。由于串的运算有很多,类 StringDS 中只包含部分基本的运算。

    程序实现:

    class StringDS
        {
            private char[] data;//用来存放字符串中的字符
    
            //构造一
            public StringDS(char[] array)
            {
                data = new char[array.Length];
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = array[i];
                }
            }
            //构造二
            public StringDS(string str)
            {
                data = new char[str.Length];
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = str[i];
                }
            }
    
            //根据索引访问字符的索引器
            public char this[int index]
            {
                get { return data[index]; }
            }
            //获得长度
            public int GetLength()
            {
                return data.Length;
            }
            //比较字符串
            /// <summary>
            /// 如果两个字符串一样 返回0
            /// 如果当前字符串小于s 返回-1
            /// 如果当前字符串大于s 返回1
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public int Compare(StringDS s)
            {
                //取得两个字符串中 长度更小的字符串的长度
                int len = this.GetLength() < s.GetLength() ? this.GetLength() : s.GetLength();
                int index = -1;//存储不相等的字符的索引的位置
                for (int i = 0; i < len; i++)
                {
                    if (this[i]!=s[i])
                    {
                        index = i;
                        break;
                    }
                }
                if (index!=-1)
                {
                    if (this[index] > s[index])
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    if (this.GetLength() == s.GetLength())
                    {
                        return 0;
                    }
                    else
                    {
                        if (this.GetLength() > s.GetLength())
                        {
                            return 1;
                        }
                        else
                        {
                            return -1;
                        }
                    }
                }
            }
    
            public StringDS SubString(int index, int length)
            {
                char[] newData = new char[length];
                for (int i = index; i < index+length; i++)
                {
                    newData[i - index] = data[i];
                }
                return new StringDS(newData);
            }
    
            public static StringDS Concat(StringDS s1, StringDS s2)
            {
                char[] newData = new char[s1.GetLength() + s2.GetLength()];
                for (int i = 0; i < s1.GetLength(); i++)
                {
                    newData[i] = s1[i];
                }
                for (int i = s1.GetLength(); i < newData.Length; i++)
                {
                    newData[i] = s2[i - s1.GetLength()];
                }
                return new StringDS(newData);
            }
    
            public int IndexOf(StringDS s)
            {
                for (int i = 0; i <= this.GetLength()-s.GetLength(); i++)
                {
                    bool isEqual = true;
                    for (int j = i; j < i+s.GetLength(); j++)
                    {
                        if (this[j] != s[j-i])
                        {
                            isEqual = false;
                        }
                    }
                    if (isEqual)
                    {
                        return i;
                    }
                    else
                    {
                        continue;
                    }
                }
                return -1;
            }
    
            public string ToString()
            {
                return new string(data);
            }
        }

    实例:

    StringDS s=new StringDS("I am a teacher");
                StringDS i=new StringDS("excellent");
                StringDS r=new StringDS("student");
    
                Console.WriteLine(s.GetLength());
                Console.WriteLine(i.GetLength());
                Console.WriteLine(r.GetLength());
    
                StringDS s2 = s.SubString(8, 4);
                Console.WriteLine(s2.ToString());
    
                Console.ReadKey();
    结果:

    image


    数组

         数组是一种常用的数据结构,可以看作是线性表的推广。数组作为一种数据结构,其特点是结构中的数据元素可以是具有某种结构的数据,甚至可以是数组,但属于同一数据类型。数组在许多高级语言里面都被作为固定类型来使用。
         数组是 n(n≥1)个相同数据类型的数据元素的有限序列。一维数组可以看作是一个线性表,二维数组可以看作是“数据元素是一维数组”的一维数组,三维数组可以看作是“数据元素是二维数组”的一维数组,依次类推。
         C#支持一维数组、多维数组及交错数组(数组的数组)。所有的数组类型都隐含继承自 System.Array。Array 是一个抽象类,本身又继承自 System.Object。所以,数组总是在托管堆上分配空间,是引用类型。任何数组变量包含的是一个指向数组的引用,而非数组本身。当数组中的元素的值类型时,该类型所需的内存空间也作为数组的一部分而分配;当数组的元素是引用类型时,数组包含是只是引用。

    Array类中的常用方法

    //判断 Array 是否具有固定大小。
        public bool IsFixedSize { get; }
        //获取 Array 元素的个数。
        public int Length { get; }
        //获取 Array 的秩(维数)。
        public int Rank { get; }
        //实现的 IComparable 接口,在.Array 中搜索特定元素。
        public static int BinarySearch(Array array, object value);
        //实现的 IComparable<T>泛型接口,在 Array 中搜索特定元素。
        public static int BinarySearch<T>(T[] array, T value);
        //实现 IComparable 接口,在 Array 的某个范围中搜索值。
        public static int BinarySearch(Array array, int index,int length, object value);
        //实现的 IComparable<T>泛型接口,在 Array 中搜索值。
        public static int BinarySearch<T>(T[] array,int index, int length, T value);
        //Array 设置为零、 false 或 null,具体取决于元素类型。
        public static void Clear(Array array, int index, int length);
        //System.Array 的浅表副本。
        public object Clone();
        //从第一个元素开始复制 Array 中的一系列元素
        //到另一 Array 中(从第一个元素开始)。
        public static void Copy(Array sourceArray,Array destinationArray, int length);
        //将一维 Array 的所有元素复制到指定的一维 Array 中。
        public void CopyTo(Array array, int index);
        //创建使用从零开始的索引、具有指定 Type 和维长的多维 Array。
        public static Array CreateInstance(Type elementType,params int[] lengths);
        //返回 ArrayIEnumerator。
        public IEnumerator GetEnumerator();
        //获取 Array 指定维中的元素数。
        public int GetLength(int dimension);
        //获取一维 Array 中指定位置的值。
        public object GetValue(int index);
        //返回整个一维 Array 中第一个匹配项的索引。
        public static int IndexOf(Array array, object value);
        //返回整个.Array 中第一个匹配项的索引。
        public static int IndexOf<T>(T[] array, T value);
        //返回整个一维 Array 中最后一个匹配项的索引。
        public static int LastIndexOf(Array array, object value);
        //反转整个一维 Array 中元素的顺序。
        public static void Reverse(Array array);
        //设置给一维 Array 中指定位置的元素。
        public void SetValue(object value, int index);
        //对整个一维 Array 中的元素进行排序,默认使用快速排序
        public static void Sort(Array array);
  • 相关阅读:
    DataType--数值类型
    Scala中List(Map1,Map2,Map3 ....) 转成一个Map
    Scala中集合类型与java中集合类型转换
    oracle查询数据库最大连接数等信息
    kafka_2.11-0.10.2.1中的auto.offset.reset
    IOS设备信息与机型对照表
    shell 后台执行脚本
    Spark的操作列表
    hive表支持中文设置
    编译Spark2.1.2源码
  • 原文地址:https://www.cnblogs.com/moguwang/p/5265083.html
Copyright © 2011-2022 走看看