2017-09-09 13 views
0

私は例えば、私のアプリケーションのために、各指標の下に複数の値を格納するための単純な/効率的な方法を見つけようとしている:私は、その後からアクセスすることができ、静的変数としてこれを設定することができるように多次元辞書?

1 = {54, "Some string", false, "Some other string"} 
2 = {12, "Some string", true, "Some other string"} 
3 = {18, "Some string", true, "Some other string"} 

単一のインデックス値(各オブジェクト内の変数のみ)を介して、様々なオブジェクトのインスタンス。基本的には、「多次元辞書」のようなものです。

私は2次元配列を見てきましたが、1つのデータ型(Int、stringなど)に限られているようで、ハッシュマップも見ました。 1つのデータ型の問題をもう一度返すリスト変数。このための簡単な解決策に関するアドバイスをお願いします。

+2

新しいクラスを作成しますか? –

答えて

2

は、それらのエントリのクラスを定義し、オブジェクトの配列を使用します。だから、クラスのようなものかもしれません:

class Thingy { 
    private int someNumber; 
    private String someString; 
    private boolean someBool; 
    private String someOtherString; 

    public Thingy(int _someNumber, String _someString, boolean _someBool, String _someOtherString) { 
     this.someNumber = _someNumber; 
     this.someString = _someString; 
     this.someBool = _someBool; 
     this.someOtherString = _someOtherString; 
    } 

    public int getSomeNumber() { 
     return this.someNumber; 
    } 
    // ...setter if appropriate... 

    // ...add accessors for the others... 
} 

...そして、あなたの操作を行います。Pythonのの

Thingy[] thingies = new Thingy[] { 
    new Thingy(54, "Some string", false, "Some other string"), 
    new Thingy(12, "Some string", true, "Some other string"), 
    new Thingy(18, "Some string", true, "Some other string") 
}; 
0

バックボーンが重く辞書データ構造に依存して、多くの場合は、反映割り当て、アクセスすることができます__dict__属性を使用して。

class ExampleObject: 
    spam = "example" 
    title = "email title" 
    content = "some content" 

obj = ExampleObject() 

print obj.spam # prints "example" 

print obj.__dict__["spam"] # also prints "example" 

はちょうどあなたのためにそこの代替オプションを投げる:あなたは、不要のJava特異性のかなり多くを減らすことが頻繁にPythonで、次のようなものを複製するので、ダースの辞書やにアクセスするために持っているモデルを持っている場合。

関連する問題