2014-12-03 87 views
-1

我正在使用c#语言并在基于控制台的应用程序上执行此操作。我遇到的问题是按字母顺序排序。我自己添加产品和数量,因此没有固定的数组列表。我想知道如何根据我进入控制台进行排序。这是否有意义?我会在哪里输入代码来执行此操作?我已经尝试,因为你可以看到我在这里突出显示它。但它会出现错误,如{“指定的投射无效”}和“当从一个数字投射时,该值必须小于无穷大。在C#中对ArrayList进行排序#

编辑:程序现在只显示说明和价格,我试图让它也显示数量。我试图输入“int q”,然后quantity = q,但我得到“Error 2 Type'TheosVendingMachine.Merchandise'的错误已经定义了一个名为'Merchandise'的成员参数类型!

 public string getDescription() 
    { 
     return this.description; 
    } 
    public double getPrice() 
    { 
     return this.price; 
    } 
    public int getQuantity() 
    { 
     return this.quantity; 
    } 
    //a simple constructor 
    public Merchandise(string n, double id) 
    * { description = n; price = id; }* 


    // a simple ToString, always good to have around! 
    public override string ToString() 
    { return description + " (£ " + price + ")" + quantity; } 



public class Merchandise 
{ 
    private string description; 
    private double price; 
    public Merchandise(string aDescription, double aPrice) 
    { 
     this.description = aDescription; 
     this.price = aPrice; 
    } 
    public string getDescription() 
    { 
     return this.description; 
    } 
    public double getPrice() 
    { 
     return this.price; 
    } 
    public override bool Equals(object other) 
    { 
     bool result; 
     if (other == null) 
     { 
      result = false; 
     } 
     else 
     { 
      Merchandise merchandise = (Merchandise)other; 
      result = (this.description.Equals(merchandise.description) && this.price == merchandise.price); 
     } 
     return result; 
    } 
public override string ToString() 
    { 
     return this.description + " at £ " + this.price; 
    } 
} 

}

+6

为什么地球上你首先使用'ArrayList'? 'List '是.NET 2.0中的替代品,'ArrayList'在很长一段时间内一直处于“不使用”列表中。 – BradleyDotNET 2014-12-03 18:15:17

+2

因为我对这个原谅我很陌生,所以我这样做是为了课程,讲师是在1000年后,所以这是我所能获得的所有帮助。 – NoobCoder 2014-12-03 18:16:00

+1

你可以在这个问题上增加一段话,说明你实际上想要做什么?这会让我们知道你想要完成什么,并让我们能够更好地指引你朝着正确的方向前进。 – 2014-12-03 18:16:59

回答

0

只是创建一个列表。这很容易。

//Instantiate List 
List<string> itemList = new List<string>(); 

//Create List 
itemList.Add("Zomato"); 
itemList.Add("Carrot"); 
itemList.Add("Banana"); 

//Sort List 
itemList.Sort(); 

//Display List 
foreach(string item in itemList){ 
    Console.WriteLine(item); 
} 

Console.ReadKey(); 
+0

这是如何让我进入我自己的产品在我的控制台应用程序? – NoobCoder 2014-12-03 18:35:25

+0

这是一个如何使用列表的示例。您将其替换为列表。如果这个类是可比较的,那么调用Sort就很简单,如你所见。如果它没有人可以使用Linq或者使其具有可比性.. – TaW 2014-12-03 18:48:54

0

即使使用老式的ArrayList你仍然可以使用它的方法Sort,只要你的类implentes IComparable

由于您没有向我们展示类Merchandise我只能向您展示一个示例。

这里是一个你的商品类制成IComparable

public class Merchandise : IComparable 
{ 
    private string description; 
    private double price; 
    private int quantity; 

    public Merchandise(string aDescription, double aPrice) 
    { 
     this.description = aDescription; 
     this.price = aPrice; 
    } 

    public Merchandise(string aDescription, double aPrice, int aQuantity) 
    { 
     this.description = aDescription; 
     this.price = aPrice; 
     this.quantity = aQuantity; 
    } 

    public string getDescription() { return this.description; } 
    public double getPrice()  { return this.price;  } 
    public int getQuantity()  { return this.quantity;  } 

    public override bool Equals(object other) 
    { 
     bool result; 
     if (other == null) result = false; 
     else 
     { 
      Merchandise merchandise = (Merchandise)other; 
      result = (this.description.Equals(merchandise.description) 
         && this.price == merchandise.price); 
     } 
     return result; 
    } 

    public int CompareTo(object o2) 
    { 
     if (o2 == null) return 1; 
     Merchandise m2 = o2 as Merchandise; 
     if (m2 != null) return this.description.CompareTo(m2.description); 
     else throw new ArgumentException("Object is not Merchandise "); 
    } 


    public override string ToString() 
    { 
     return description + " (£ " + price + ")" + quantity; 
    } 
} 

尤其要注意CompareTo方法! IComparable只需要这种方法,现在你可以在课堂上使用ArrayList.Sort。如果真的很简单,因为它可以依赖字符串成员为Comparable

更复杂的比较将直接编码;在你的程序中,你可能想使用名称成员而不是ID进行比较..

+0

感谢您的帮助,这是我的商品类,如果它有帮助吗? – NoobCoder 2014-12-04 12:42:51

+0

所有你需要做的就是将'CompareTo'方法添加到你的类中,就像它一样 - 只将成员'MerchandiseID'改为'description'!还要将IComparable添加到类声明中!然后排序应该工作。 – TaW 2014-12-04 14:57:23

+0

非常感谢它的工作! – NoobCoder 2014-12-04 16:14:53

相关问题