1
私はJavaに慣れていますが、私は小さな問題があります。 String
を返す関数と、int
を返す関数があり、このようなリストに入れなければなりません。({String, int},{String, int})
どうすればそれが最善でしょうか?Javaの問題specefic構造体のリストを埋め込む
私はJavaに慣れていますが、私は小さな問題があります。 String
を返す関数と、int
を返す関数があり、このようなリストに入れなければなりません。({String, int},{String, int})
どうすればそれが最善でしょうか?Javaの問題specefic構造体のリストを埋め込む
@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);
}
}
あなたは、このためにBeanクラスを作るか、HashMapの.USEコレクションAPIを使用する必要があります] –
どちらか '地図<文字列、整数>'や '一覧'。 –
私はこの問題を理解しているかどうか分かりませんが、PythonにはJavaのようなタプルはありません。いくつかの種類のオブジェクトに2つの値をカプセル化し、それらをListに追加します。 – duffymo