ここでは、それぞれのアプローチの実装方法について説明します。
Apache Common Lang APIにはPair
クラスがあります。これは技術的には「標準」ライブラリの一部ではありません。 http://commons.apache.org/proper/commons-lang/からダウンロードできます。
Pair
のドキュメントはhereです。
あなたはこのようにそれを使用することができます:
// in the method
public Pair<ArrayList, ArrayList> yourMethod() {
...
return ImmutablePair.of(yourArrayList1, yourArrayList2);
}
// when you call the method
Pair<ArrayList, ArrayList> pair = yourMethod();
pair.getLeft(); // gets you the first array list
pair.getRight(); // gets you the second array list
また、あなたの第二のアプローチがやろうとしているもの、本質的であるあなた自身のPair
クラスを作成することができます。
this postの例を示します。
public class Pair<K, V> {
private final K element0;
private final V element1;
public static <K, V> Pair<K, V> createPair(K element0, V element1) {
return new Pair<K, V>(element0, element1);
}
public Pair(K element0, V element1) {
this.element0 = element0;
this.element1 = element1;
}
public K getElement0() {
return element0;
}
public V getElement1() {
return element1;
}
}
あなたはこのようにそれを使用することができます:
// in the method
public Pair<ArrayList, ArrayList> yourMethod() {
...
return Pair.createPair(yourArrayList1, yourArrayList2);
}
// when you call the method
Pair<ArrayList, ArrayList> pair = yourMethod();
pair.getElement0(); // gets you the first array list
pair.getElement1(); // gets you the second array list
「ペア」クラスが機能するはずです。それを使用する方法を学ぶために、ドキュメントを読んでください。 – 4castle
@ 4castle私は第2の選択肢を開始しました。それもうまくいくと思いますか? – Eddie
2つめのオプションも使えますが、基本的には 'Pair'クラスを再開発しています。 – 4castle