2014-12-03 88 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

相關問題