本帖最后由 邓明 于 2014-6-5 15:27 编辑
我在学习数组的时候也在参考其他相关书籍同步在学,其中有关于多维数组的部分我遇到了这样的一个练习,与大家分享,并且有几个问题想请教下:
题目的大意是这样的:有几本图书,我们知道书名、价格和作者,需要编写段代码按照分别按照书名、价格、作者姓名进行排序,并显示出排序结果。
我的思路是这样的:构建多维数组,然后根据不同的排序方式调用不同的方法来进行排序,具体代码如下:
Book类:
- <p>
- <p class="blockcode">class Book
- {
- //图书的属性
- public string strName;//书名
- public double dblPrice;//价格
- public string strAuthor;//作者</p><p> </p><p> public Book(string _strName, double _dblPrice, string _strAuthor)
- {
- this.strName = _strName;
- this.dblPrice = _dblPrice;
- this.strAuthor = _strAuthor;
- }</p></p>
复制代码 BookList类:- <p><p> class BookList
- {
- /// <summary>
- /// 按照书名显示一个图书数组中的多本图书
- /// </summary>
- /// <param name="arrBooks">图书数组</param></p><p></p><p> public static void DisplayByName(Book[] arrBooks)
- {
- //获取图书数量,用于动态建立“书名”数组
- int bookNumber = arrBooks.GetUpperBound(0) - arrBooks.GetLowerBound(0) + 1;</p><p></p><p> //用CreateInstance方法动态建立“书名”数组
- int[] lengths = new int[] { bookNumber };
- int[] lowerBounds = new int[] { 0 };
- Array arrNames=Array.CreateInstance(Type.GetType("System.String"),lengths,lowerBounds);
- //为“书名”赋值
- for (int i = arrBooks.GetLowerBound(0); i <= arrBooks.GetUpperBound(0); i++)
- {
- arrNames.SetValue(arrBooks[i].strName, i);
- }
- //对于arrBooks数组针对书名arrNames关键字进行排序
- Array.Sort(arrNames, arrBooks);
- //对排序完成后的arrBooks数组将各个元素显示和输出
- foreach (Book item in arrBooks)
- {
- Console.WriteLine("{0} {1} {2}", item.strName, item.dblPrice, item.strAuthor);
- }
- }
- /// <summary>
- /// 按照图书价格显示多本图书
- /// </summary>
- /// <param name="arrBooks">图书数组</param>
- public static void DisplayByPrice(Book[] arrBooks)
- {
- //获取图书数量,用于动态建立“价格”数组
- int bookNumber = arrBooks.GetUpperBound(0) - arrBooks.GetLowerBound(0) + 1;
- //用CreateInstance方法动态建立“价格”数组
- int[] lengths = new int[] { bookNumber };
- int[] lowerBounds = new int[] { 0 };
- Array arrPrice = Array.CreateInstance(Type.GetType("System.Double"), lengths, lowerBounds);
- //为“价格”赋值
- for (int i = arrBooks.GetLowerBound(0); i <= arrBooks.GetUpperBound(0); i++)
- {
- arrPrice.SetValue(arrBooks[i].dblPrice, i);
- }
- //对于arrBooks数组针对价格arrPrice关键字进行排序
- Array.Sort(arrPrice, arrBooks);
- //对排序完成后的arrBooks数组将各个元素显示和输出
- foreach (Book item in arrBooks)
- {
- Console.WriteLine("{0} {1} {2}", item.strName, item.dblPrice, item.strAuthor);
- }
- }
- /// <summary>
- /// 按照图书作者显示多本图书
- /// </summary>
- /// <param name="arrBooks">图书数组</param>
- public static void DisplayByAuthor(Book[] arrBooks)
- {
- //获取图书数量,用于动态建立“作者”数组
- int bookNumber = arrBooks.GetUpperBound(0) - arrBooks.GetLowerBound(0) + 1;
- //用CreateInstance方法动态建立“作者”数组
- int[] lengths = new int[] { bookNumber };
- int[] lowerBounds = new int[] { 0 };
- Array arrAuthor = Array.CreateInstance(Type.GetType("System.String"), lengths, lowerBounds);
- //为“作者”赋值
- for (int i = arrBooks.GetLowerBound(0); i <= arrBooks.GetUpperBound(0); i++)
- {
- arrAuthor.SetValue(arrBooks[i].strAuthor, i);
- }
- //对于arrBooks数组针对作者arrAuthor关键字进行排序
- Array.Sort(arrAuthor, arrBooks);
- //对排序完成后的arrBooks数组将各个元素显示和输出
- foreach (Book item in arrBooks)
- {
- Console.WriteLine("{0}{1}{2}", item.strName, item.dblPrice, item.strAuthor);
- }
- }
- }</p></p>
复制代码
|