トップページ > プログラム > 2015年11月21日 > ZYL4H3PD

書き込み順位&時間帯一覧

5 位/175 ID中時間01234567891011121314151617181920212223Total
書き込み数0101000000010002002100008



使用した名前一覧書き込んだスレッド一覧
デフォルトの名無しさん
★★Java質問・相談スレッド175★★ [転載禁止]©2ch.net

書き込みレス一覧

★★Java質問・相談スレッド175★★ [転載禁止]©2ch.net
253 :デフォルトの名無しさん[sage]:2015/11/21(土) 01:04:41.40 ID:ZYL4H3PD
>>240
> c++とかならpairをいれた配列をソートすればいいんですが
Javaでも同じ
static class Pair {
final double first;
final int second;
Pair(double first, int second) { this.first = first; this.second = second; }
}
public static void main(String[] args) {
double[] a = {2.0, 1.2, 8.5, 1.2};
int[] b = {1, 2, 3, 4};
ArrayList<Pair> list = new ArrayList<>();
for (int i = 0; i < a.length; i++) list.add(new Pair(a[i], b[i]));
Collections.sort(list, new Comparator<Pair>() {
public int compare(Pair a, Pair b) { return a.first == b.first ? 0 : (a.first < b.first ? -1 : 1 ); }
});
for (Pair p : list) System.out.println(p.second); //=> 2 4 1 3
}
標準でPairがないので、自分で作るか、Androidならandroid.util.Pair,
commons-langが使えるならorg.apache.commons.lang3.tuple.Pair
★★Java質問・相談スレッド175★★ [転載禁止]©2ch.net
258 :デフォルトの名無しさん[sage]:2015/11/21(土) 03:41:13.57 ID:ZYL4H3PD
Java8ずいぶん変わってるなw
class Pair {
final double first;
final int second;
Pair(double first, int second) { this.first = first; this.second = second; }
}
double[] a = {2.0, 1.2, 8.5, 1.2};
int[] b = {1, 2, 3, 4};
ArrayList<Pair> list = new ArrayList<Pair>() { { for (int i = 0; i < a.length; i++) add(new Pair(a[i], b[i])); } };
list.sort(Comparator.comparing(x -> x.first));
list.forEach(p -> System.out.println(p.second)); //=> 2 4 1 3
★★Java質問・相談スレッド175★★ [転載禁止]©2ch.net
261 :デフォルトの名無しさん[sage]:2015/11/21(土) 11:13:41.85 ID:ZYL4H3PD
>>260
計測したのは立派だがこんなマイクロベンチの結果でいいとかダメとかいうんじゃない。
この結果ならわかりやすさの方が100倍重要。
boxing/unboxingすれば多少遅くなるのは自明。それでも他の意味のある処理が入れば誤差で消える程度の差でしかない。
★★Java質問・相談スレッド175★★ [転載禁止]©2ch.net
264 :デフォルトの名無しさん[sage]:2015/11/21(土) 15:57:46.32 ID:ZYL4H3PD
class Pair {
final double first;
final int second;
Pair(double first, int second) { this.first = first; this.second = second; }
}
double[] a = {2.0, 1.2, 8.5, 1.2};
int[] b = {1, 2, 3, 4};
IntStream.range(0, a.length).mapToObj(i -> new Pair(a[i], b[i])).sorted(Comparator.comparing(x -> x.first)).forEach(p -> System.out.println(p.second)); //=> 2 4 1 3
★★Java質問・相談スレッド175★★ [転載禁止]©2ch.net
265 :デフォルトの名無しさん[sage]:2015/11/21(土) 15:59:47.79 ID:ZYL4H3PD
Stream APIすげー
★★Java質問・相談スレッド175★★ [転載禁止]©2ch.net
267 :デフォルトの名無しさん[sage]:2015/11/21(土) 18:10:47.18 ID:ZYL4H3PD
int[] sortedB = IntStream.range(0, a.length).mapToObj(i -> new Pair(a[i], b[i])).sorted(Comparator.comparing(p -> p.first)).mapToInt(p -> p.second).toArray();
Arrays.stream(sortedB).forEach(System.out::println); //=> 2, 4, 1, 3
★★Java質問・相談スレッド175★★ [転載禁止]©2ch.net
268 :デフォルトの名無しさん[sage]:2015/11/21(土) 18:37:27.23 ID:ZYL4H3PD
Streamを使うと >>256 はこう書ける
double[] a = {2.0, 1.2, 8.5, 1.2};
int[] b = {1, 2, 3, 4};
int[] sortedIndices = IntStream.range(0, a.length).mapToObj(i -> new Integer(i)).sorted((x, y) -> Double.compare(a[x], a[y])).mapToInt(Integer::intValue).toArray();
Arrays.stream(sortedIndices).forEach(i -> System.out.printf("%d %f %d\n", i, a[i], b[i])); //=> 2, 4, 1, 3
★★Java質問・相談スレッド175★★ [転載禁止]©2ch.net
270 :デフォルトの名無しさん[]:2015/11/21(土) 19:34:54.58 ID:ZYL4H3PD
それがいいね


※このページは、『2ちゃんねる』の書き込みを基に自動生成したものです。オリジナルはリンク先の2ちゃんねるの書き込みです。
※このサイトでオリジナルの書き込みについては対応できません。
※何か問題のある場合はメールをしてください。対応します。