2017-02-09 51 views
0

我是C#的新手,來自Java,語法有點不同。 我想返回一個arrayList中元素的雙精度值。我想返回ArrayList中元素的雙重屬性

public ArrayList vehicleList { get; set; } 

class Vehicle 
{ 
    //A vehicle can be a car and must have an ID, a price and a licenseplate number. 
    public int iD { get; set; } 
    public double price { get; set; } 
    public string licensePlate { get; set; } 
    public typeVehicle type; 

    //Constructor of vehicle, type of vehicle will be parsed from string to enum. 
    public Vehicle(int iD, string typeName, double price, string license) 
    { 
     this.iD = iD; 
     this.type = (typeVehicle)Enum.Parse(typeof(typeVehicle), typeName); 
    } 
} 

    //Get the price of the vehicle with parameter ID. 
    public double getPriceVehicle(int iD) 
    { 
     if (iD >= 0 && iD < vehicleList.Count) 
     { 
      foreach (Vehicle vehicle in vehicleList) 
      { 
       return vehicleList.Contains(iD).price; 
      } 
     } 
    } 

的問題是,我上回線得到一個錯誤,我不能在網上找到任何解決方案這完全是。任何幫助,將不勝感激。

+0

謝謝大家在這篇文章上的回覆。我最終選擇了最簡單的解決方案,因爲那是我理解的那個。使用LINQ的答案我也理解,但這是我在課程中不需要學習的東西。 我故意讓iD和陣列中的位置相同,因爲現在它更容易,這也意味着第一輛車將具有00的iD。 – Aleathia

回答

1

參數iD是數組的索引嗎?

我的建議是使用List而不是ArrayList。

private List<Vehicle> vehicleList; 

public double getVehiclePrice(int index){ 
    if(index >= 0 && index < vehicleList.Count){ 
     return vehicleList[index].price; 
    } 
} 
+0

這工作,非常感謝。 – Aleathia

2
List<Vehicle> vehicleList { get; set; } 

//Get the price of the vehicle with parameter ID. 
public double GetPriceVehicle(int id) 
{ 
    var result = vehicleList.FirstOrDefault(v => v.ID == id); 
    if (result == null) 
    { 
     throw new NotImplementedException(String.Format("Vehicle with ID={0} was not found", id)); //todo: put in the logic you want for when no vehicle has the given id 
    } 
    return result.Price; 
} 

1)使用類型車輛(System.Collections.Generic.List<Vehicle>的通用列表),而不是一個ArrayList。請參閱這裏瞭解更多:ArrayList vs List<> in C#

2)您使用ID作爲ID和索引。即,ID是分配給特定車輛的值,不論您是否添加或移除其他車輛都應該保持相同;即它是一個標識符。索引是列表中項目的編號。舉一個例子,如果我在一輛車通過的車隊排隊,隊列前面的車輛被拉走,然後他們離開我的索引變化;即我從排隊中的第五位變成了第四位。另一方面,如果我正在與我的保險公司交談,他們說我的車的檔案號爲#3265,他們是否會失去一位客戶,我不希望他們寫信給我,說我的ID現在是#3264,我現在需要更新帶有這個參考號的所有文檔。

3)這裏的Where邏輯使用了一種稱爲lambda表達式的東西;即我們正在通過列表中的所有車輛搜索具有與傳入該功能的ID相匹配的ID的車輛。這個lambda表達式可以放在一個實際的Where語句中(例如Where(v => v.ID == id).FirstOrDefault()),但FirstOrDefault允許我們直接在其中包含lambda表達式。 https://msdn.microsoft.com/en-us/library/bb397687.aspx

4)FirstOrDefault說,如果我們找到一個結果,我們停止搜索(即我們並不希望在同一個ID的第二輛,所以不要浪費時間去尋找,我們發現一個後)。 FirstOrDefault的Default部分表示如果我們沒有找到匹配,則返回此類型的默認值; default(Vehicle);給定Vehicle是參考類型(即類),爲空。 https://msdn.microsoft.com/pt-br/library/bb909042(v=vs.90).aspx

5)如果結果爲空,我們不能返回result.Price;那將會出錯,因爲空對象沒有屬性。錯誤很好;但不一定有幫助;更好地拋出我們自己詳細說明問題的錯誤;我們可以添加邏輯來在調用此方法的任何代碼中以合理的方式處理該錯誤。 https://msdn.microsoft.com/en-us/library/ms173163.aspx

6)我也改變了一些變量/屬性名稱;例如iDID。一般來說,任何公衆都應該在pascalCase中;任何私人/本地應該在camelCase中。 https://msdn.microsoft.com/en-us/library/ms229043(v=vs.100).aspx

7)我沒有在這裏實現,但更好的選擇可能是一個通用的字典(Dictionary<long, Vehicle>);由於此數據結構已針對快速檢索進行了優化,因此不是通過搜索列表中的每個項目來查找匹配項,而是使用散列表快速查找與給定項/ ID相關聯的數據。
http://geekswithblogs.net/blackrabbitcoder/archive/2011/06/16/c.net-fundamentals-choosing-the-right-collection-class.aspx

2

Contains返回一個布爾值。布爾沒有價格成員。

在Java風格的你可以這樣做:

//Get the price of the vehicle with parameter ID. 
public double getPriceVehicle(int iD) 
{ 
    if (iD >= 0 && iD < vehicleList.Count) 
    { 
     foreach (Vehicle vehicle in vehicleList) 
     { 
      if (vehicle.iD == iD) return vehicle.price; 
     } 
    } 
    return 0; 
} 

但在C#中,我們有LINQ的力量!

public List<Vehicle> vehicleList { get; set; } 

class Vehicle 
{ 
    //A vehicle can be a car and must have an ID, a price and a licenseplate number. 
    public int iD { get; set; } 
    public double price { get; set; } 
    public string licensePlate { get; set; } 
    public typeVehicle type; 

    //Constructor of vehicle, type of vehicle will be parsed from string to enum. 
    public Vehicle(int iD, string typeName, double price, string license) 
    { 
     this.iD = iD; 
     this.type = (typeVehicle)Enum.Parse(typeof(typeVehicle), typeName); 
    } 
} 

    //Get the price of the vehicle with parameter ID. 
    public double getPriceVehicle(int iD) 
    { 
     if (iD < 0 || iD >= vehicleList.Count) 
      throw new ArgumentOutOfRangeException("iD"); 

     var vehicle = vehicleList.FirstOrDefault(v => v.iD == iD); 
     return vehicle == null ? double.NaN : vehicle.price; 
    } 
+1

可以縮短爲 public double? getPriceVehicle(int iD) return vehicleList.FirstOrDefault(v => v.iD == iD)?。price; } – Dmitry

+0

@Dmitry - 在後面的版本中,是的! – hoodaticus

相關問題