2017-05-05 11 views
1

私はJavaに慣れていますが、私は小さな問題があります。 Stringを返す関数と、intを返す関数があり、このようなリストに入れなければなりません。({String, int},{String, int})どうすればそれが最善でしょうか?Javaの問題specefic構造体のリストを埋め込む

+0

あなたは、このためにBeanクラスを作るか、HashMapの.USEコレクションAPIを使用する必要があります] –

+2

どちらか '地図<文字列、整数>'や '一覧'。 –

+0

私はこの問題を理解しているかどうか分かりませんが、PythonにはJavaのようなタプルはありません。いくつかの種類のオブジェクトに2つの値をカプセル化し、それらをListに追加します。 – duffymo

答えて

0

@duffymoがコメントで説明している問題であれば、Javaはそれをしないと思います。しかし、2つのフィールドを持つクラスを作成することで、問題を一巡させることができます。

private String methodForString() { 
    String result = new String(); 
    // ... your code which manipulates result 
    return result; 
} 

private int methodForInt() { 
    int result = 0; 
    // ... your code which manipulates result 
    return result; 
} 

class MyClass { 
    String returnString; 
    int returnInt; 

    public String getReturnString() { 
     return returnString; 
    } 

    public void setReturnString(String returnString) { 
     this.returnString = returnString; 
    } 

    public int getReturnInt() { 
     return returnInt; 
    } 

    public void setReturnInt(int returnInt) { 
     this.returnInt = returnInt; 
    } 
} 

private void initializingMethod() { 
    List<MyClass> list = new ArrayList<>(); 
    int numberItems = 100;// this represents the number of items you want to 
          // put in your list 
    for (int i = 0; i < numberItems; i++) { 
     MyClass myClass = new MyClass(); 
     myClass.setReturnInt(methodForInt()); 
     myClass.setReturnString(methodForString()); 
     list.add(myClass); 
    } 
} 
関連する問題