C#数据结构(三)----串和数组

来源:互联网 发布:ajax发送表单数据 编辑:程序博客网 时间:2024/05/16 23:01

1)C#数据结构(一)----线性表
2)C#数据结构(二)----栈和队列

一、关于C#中的字符串:

1)串是由连续存储的字符组成
2)C#中的串具有恒定不变的特性,即 一旦被创建,就不能改变长度或者改变其中任何的字符。
3)串的连接、插入和删除等操作都是生成了新串而没有改变原串。
4)继承自 System.object。所以是引用类型(int,bool,char 等都是struct 不是class,是值类型)。
5)System.String 是密封类,所以不能被继承。
6)虽然System.String 是引用类型,但C#中将String 看作是基元类型,所以不用 new操作符创建实例,而是使用字符串驻留的机制。
7)System.String继承自 IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string>。
8)C#提供了StringBuilder类型来支持高效地动态创建字符串。

下面是自定义一个string类,类中包含一个字段,用以存放字符序列的数组,还有一些常用的串操作。

String
 public class StringDS
    {
       
private char[] data;//char数组
       
//索引器
       public char this[int index]
        {
            
get 
            {
                
return data[index];
            }
            
set
            {
                data[index] 
= value;
            }
        }
       
//构造函数
       public StringDS(char[] arr)
        {
            data 
= new char[arr.Length];
            
for (int i = 0; i < arr.Length; i++)
            {
                data[i] 
= arr[i];
            }
        }
       
//构造函数 
       public StringDS(int len)
        {
            
char[] arr = new char[len];
            data 
= arr;
        }
       
//求串长 
       public int GetLength()
        {
            
return data.Length;
        } 
       
//串比较 
       public int Compare(StringDS s) 
       {  
          
int len=((this.GetLength()<=s.GetLength())? 
          
this.GetLength():s.GetLength()); 
          
int i = 0
          
for (i = 0; i < len; ++i) 
          { 
              
if (this[i] != s[i]) 
              { 
                  
break
              } 
          } 
          
if (i <= len)
          {
              
if (this[i] < s[i])
              {
                  
return -1;
              }
              
else if (this[i] > s[i])
              {
                  
return 1;
              }
          }
          
else if (this.GetLength() == s.GetLength())
          {
              
return 0;
          }
          
else if (this.GetLength() < s.GetLength())
          {
              
return -1;
          }

          
return 1;
       } 
       
//求子串 
       public StringDS SubString(int index, int len) 
       { 
            
if ((index<0|| (index>this.GetLength()-1|| (len<0|| (len>this.GetLength()-index)) 
            { 
                Console.WriteLine(
"Position or Length is error!"); 
                
return null
            } 

            StringDS s 
= new StringDS(len); 

            
for (int i = 0; i < len; ++i) 
            { 
                s[i] 
= this[i + index-1]; 
            } 

            
return s; 
       } 
       
//串连接 
       public StringDS Concat(StringDS s)
       {
           StringDS s1 
= new StringDS(this.GetLength() +s.GetLength());
           
for (int i = 0; i < this.GetLength(); ++i)
           {
               s1.data[i] 
= this[i];
           }

           
for (int j = 0; j < s.GetLength(); ++j)
           {
               s1.data[
this.GetLength() + j] = s[j];
           }

           
return s1;
       } 
       
//串插入 
       public StringDS Insert(int index, StringDS s) 
         { 
           
int len = s.GetLength(); 
           
int len2 = len + this.GetLength(); 
           StringDS s1 
= new StringDS(len2); 
           
if (index < 0 || index > this.GetLength() - 1
           { 
               Console.WriteLine(
"Position is error!"); 
               
return null
           } 
           
for (int i = 0; i < index; ++i) 
           { 
               s1[i] 
= this[i]; 
           } 
           
for(int i = index; i < index + len ; ++i) 
           { 
               s1[i] 
= s[i - index]; 
           } 
           
for (int i = index + len; i < len2; ++i) 
           { 
               s1[i] 
= this[i - len]; 
           }
           
return s1;
         }
       
//串删除 
       public StringDS Delete(int index, int len)
         {
             
if ((index < 0|| (index > this.GetLength() - 1)
                 
|| (len < 0|| (len > this.GetLength() - index))
             {
                 Console.WriteLine(
"Position or Length is error!");
                 
return null;
             }

             StringDS s 
= new StringDS(this.GetLength() - len);

             
for (int i = 0; i < index; ++i)
             {
                 s[i] 
= this[i];
             }

             
for (int i = index + len; i < this.GetLength(); ++i)
             {
                 s[i] 
= this[i];
             }

             
return s;
         } 
       
//串定位 
       public int Index(StringDS s) 
          { 
            
if (this.GetLength() < s.GetLength()) 
            { 
                Console.WriteLine(
"There is not string s!"); 
                
return -1
            } 
 
            
int i = 0
            
int len = this.GetLength() - s.GetLength(); 
            
while (i < len) 
            { 
                
if (this.Compare(s) == 0
                {
                    
break;
                }
            }

            
if (i <= len)
            {
                
return i;
            }

            
return -1;
          } 
    }

 

二、数组
     1) 数组是 n(n≥1)个相同数据类型的数据元素的有限序列。
     2) 具有固定格式和数量
     3)每一个数据元素通过唯一的下标来标识和访问
     4)一经定义,每一维的大小及上下界都不能改变,所以,在数组上不能进行插入、删除数据元素等操作
     5) 数组采用顺序存储结构来存储数组中的数据元素
     6) 数组上的操作一般有:
        1、取值操作:给定一组下标,读其对应的数据元素;
        2、赋值操作:给定一组下标,存储或修改与其对应的数据元素;
        3、清空操作:将数组中的所有数据元素清除;
        4、复制操作:将一个数组的数据元素赋给另外一个数组;
        5、排序操作:对数组中的数据元素进行排序,这要求数组中的数据元素是可排序的;
        6、反转操作:反转数组中数据元素的顺序。
三、C#中数组
     1) C#支持一维数组、多维数组及交错数组。
     2)所有的数组类型都隐含继承自 System.Array。
     3)Array 是一个抽象类,继承自 System.Object,ICloneable, IList, ICollection, IEnumerable,所以数组是引用类型。
     4)C#除了能创建静态数组外,还可以创建动态数组,通过使用 Array 的静态方法 CreateInstance 方法来实现。

Array
 public class myArray
    {
        
public void Run()
        {
            
//静态数组
            string[] arr = new string[5];
            
for (int i = 0; i < 5; i++)
            {
                arr[i] 
= "val"+i;
            }
            
//动态数组
            Array arr1 = Array.CreateInstance(typeof(string),5);
            
            
for (int i = 0; i < 5; i++)
            {
                arr1.SetValue(
"val" + i, i);
            }
            
//输出
            Console.WriteLine("-------arr--------");
            
foreach (string i in arr)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine(
"-------arr1--------");
            
foreach (string i in arr1)
            {
                Console.WriteLine(i);
            }
            
        }
    }

 

 

原创粉丝点击