2017-02-09 1 views
0

私はC#で新しく、Javaから来ていますが、構文は少し異なります。 arrayList内の要素のdoubleを返そうとしています。ArrayList内の要素のdoubleプロパティを返したい

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とその配列の場所を同じにしましたが、今はちょっと簡単です。これは、最初の車両のIDが00になることを意味します。 – Aleathia

答えて

1

パラメータiDは配列のインデックスですか?

私の提案は、ArrayListの代わりにListを使うことです。

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)の代わりにArrayList型車両の汎用リスト(System.Collections.Generic.List<Vehicle>)を使用。 ArrayList vs List<> in C#

2)あなたはIDとインデックスの両方としてIDを使用しています。すなわち、IDは、特定の車両に割り当てられた値であり、他の車両の追加または削除の有無にかかわらず同じままでなければならない。すなわちそれは識別子である。インデックスは、リスト内のアイテムの番号です。例を挙げると、私がドライブスルーで車の列に並んでいて、列の前にある車が離れていたら、インデックスの変更が残っています。つまり、キューの5番目から4番目に移動します。一方、私が保険会社に話していると、車が#3265としてファイルされていると言っても、彼らは顧客を失うべきでしょうか、私のIDは現在#3264であると書いてもらえません。この参照番号を持つ私のすべての文書を今更新する必要があります。

3)ここではWhereロジックはラムダ式と呼ばれるものを使用します。すなわち、機能に渡されたIDに一致するIDを有するものについて、リスト内のすべての車両を探索している。このラムダ式は実際のWhereステートメント(たとえばWhere(v => v.ID == id).FirstOrDefault())に置くことができますが、FirstOrDefaultはラムダ式を直接に含めることができます。 https://msdn.microsoft.com/en-us/library/bb397687.aspx

4)FirstOrDefaultは、我々は結果を見つけた場合、我々は(すなわち、私たちは)同じIDの2番目の車両を期待していないので、我々は1を見つけた後に探して時間を無駄にしない検索を停止することを言います。 FirstOrDefaultのDefault部分には、一致が見つからない場合は、この型のデフォルト値が返されます。 default(Vehicle); Vehicleが参照型(つまりクラス)である場合、nullです。 https://msdn.microsoft.com/pt-br/library/bb909042(v=vs.90).aspx

5)結果がnullの場合、result.Priceを返すことはできません。ヌルオブジェクトはプロパティを持たないため、エラーになります。エラーは問題ありません。それは必ずしも有用ではありません。問題を詳述する私たち自身の誤りを投げる方が良い。このメソッドを呼び出すすべてのコードで、エラーを適切に処理するためのロジックを追加できます。 https://msdn.microsoft.com/en-us/library/ms173163.aspx

6)変数/プロパティ名の一部も変更しました。例えばiD~ID。一般に公開されるものはすべて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はブール値を返します。 Booleanには価格のメンバーはありません。

//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の力を持っている:Javaのスタイルで

あなたはこれを行うだろう!

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

公開ダブルに短縮できますか? getPriceVehicle(int iD) { return vehicleList.FirstOrDefault(v => v.iD == iD)?. price; } – Dmitry

+0

@ドミトリー - それ以降のバージョンでは、うん! – hoodaticus

関連する問題