文章目录
- 题目 1: 创建简单的索引器
- 题目 2: 实现只读索引器
- 题目 3: 索引器与验证
- 题目 4: 多维索引器
- 题目 5: 索引器与初始化
题目 1: 创建简单的索引器
问题: 编写一个类 SimpleList,该类内部有一个 int 类型的数组 items,并实现一个索引器用于访问该数组中的元素。索引器的索引应从 0 开始。
public class SimpleList{int[]items=new int[Count];static int Count = 10;public SimpleList(){for(int i=0;i<Count;i++){items[i]=0;}}public int this[int index]{get{int item;if (index >= 0 && index < Count){item = items[index];}else{throw new IndexOutOfRangeException();}return item;}set{if(index>= 0 && index < Count){items[index] = value;}}}static void Main(string[] args){SimpleList list = new SimpleList();list[0] = 1;list[1] = 2;list[2] = 3;list[3] = 4;list[4] = 5;list[5] = 6;list[6] = 7;list[7] = 8;list[8] = 9;list[9] = 10;for (int i = 0; i < SimpleList.Count; i++){Console.WriteLine(list[i]);}Console.ReadLine();}}
题目 2: 实现只读索引器
问题: 编写一个类 ReadOnlyCollection,该类内部有一个 string 类型的列表 strings。实现一个只读索引器,用于访问列表中的元素,但不能修改这些元素。
public class ReadOnlyCollection{string[] strings = new string[daysCount];static int daysCount = 7;public ReadOnlyCollection(){for(int i=0; i<daysCount; i++){strings[i] = "Weekend";}}public string this[int index]{get{string week;if(index>=0&&index<daysCount){week = strings[index];}else{throw new IndexOutOfRangeException();}return week;}}static void Main(string[] args){ReadOnlyCollection weeks= new ReadOnlyCollection();for(int i=0; i<daysCount ; i++){Console.WriteLine(weeks[i]);}Console.ReadLine();}}
题目 3: 索引器与验证
问题: 编写一个类 ValidatedList,该类有一个 double 类型的数组 values。实现一个索引器,在设置值时确保索引在有效范围内,并且值是正数。如果设置的值不符合要求,应抛出一个异常。
public class ValidatedList{double[] values=new double[Count];static int Count = 5;public ValidatedList() { for(int i=0;i<Count; i++){values[i]=0;}}public double this[int index]{get{double item;if (index >= 0 && index < Count){item = values[index];}else{throw new Exception();}return item;}set{if(index >= 0 && index < Count){if(value>0){values[index]=value;}else{throw new IndexOutOfRangeException();}}else{throw new Exception();}}}static void Main(string[] args){ValidatedList list= new ValidatedList();list[0] = 1;list[1] = 2;list[2] = 3;list[3] = 4;list[4] = -1;for(int i=0;i<Count;i++){Console.WriteLine(list[i]);}Console.ReadLine();}}
题目 4: 多维索引器
问题: 编写一个类 Matrix,表示一个二维矩阵。实现一个索引器,允许通过两个索引(行和列)访问矩阵的元素。
public class Matrix{private int[,] array = new int[length1, length2];static int length1 = 3;static int length2 = 2;public int this[int index1,int index2]{get{if (index1 >= 0 && index1 < length1 && index2 >= 0 && index2 < length2){return array[index1,index2];}else{return 0;}}set{if (index1 >= 0 && index1 < length1 && index2 >= 0 && index2 < length2){array[index1,index2] = value;}}}static void Main(string[] args){Matrix matrix=new Matrix();matrix[1, 1] = 22;matrix[2, 1] = 44;for(int i=0; i < length1; i++){for(int j=0;j<length2; j++){Console.WriteLine("matrix[{0}][{1}]={2}", i, j, matrix[i,j]);}}Console.ReadLine();}}
题目 5: 索引器与初始化
问题: 编写一个类 CustomList,该类有一个 List 类型的字段 items。在构造函数中初始化列表,并实现一个索引器,用于访问列表中的元素。索引器应在访问列表元素之前确保索引有效无效则抛出异常。
public class CustomList{private List<int> items;public CustomList() { items = new List<int>();}public int this[int index]{get{if (index < 0 || index >= items.Count){throw new IndexOutOfRangeException($"索引{index}超出了范围");}else{return items[index];}}set{if(index<0||index>=items.Count){throw new IndexOutOfRangeException($"索引{index}超出了范围");}else{items[index] = value;}}}public void Print(){foreach(var item in items){Console.WriteLine(item);}}static void Main(string[] args){CustomList list = new CustomList();list.items.Add(1);list.items.Add(2);list.items.Add(3);//Console.WriteLine(list[1]);//Console.WriteLine(list[4]);list[0] = 3;list.Print();}}