teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

java7の場合を追記

2017/01/31 13:09

投稿

7tsuno
7tsuno

スコア310

answer CHANGED
@@ -9,4 +9,28 @@
9
9
  .findFirst().get();
10
10
 
11
11
  System.out.println(maxIndex);
12
+ ```
13
+
14
+ ~java7でしたらこんな感じですかね
15
+
16
+ ```java
17
+
18
+ int[] a = new int[] { 1, 4, 2, 3, 4, 5 };
19
+ int[] b = new int[] { 2, 1, 5, 6, 7, 0 };
20
+
21
+ int maxIndex = 0;
22
+ int maxNum = 0;
23
+
24
+ for (int i = 0; i < a.length; i++) {
25
+
26
+ if (maxNum < a[i] + b[i]) {
27
+ maxNum = a[i] + b[i];
28
+ maxIndex = i + 1;
29
+ }
30
+
31
+ }
32
+
33
+ System.out.println(maxIndex);
34
+
35
+ }
12
36
  ```

1

質問の意図を勘違いしていたので修正しました。

2017/01/31 13:09

投稿

7tsuno
7tsuno

スコア310

answer CHANGED
@@ -4,8 +4,9 @@
4
4
  int[] a = new int[] { 1, 4, 2, 3, 4, 5 };
5
5
  int[] b = new int[] { 2, 1, 5, 6, 7, 0 };
6
6
 
7
- int maxSum = IntStream.range(0, a.length).map(index -> a[index] + b[index]).max()
7
+ int maxIndex = IntStream.range(0, a.length).mapToObj(index -> index)
8
+ .sorted((index1, index2) -> (a[index2] + b[index2]) - (a[index1] + b[index1]))
8
- .getAsInt();
9
+ .findFirst().get();
9
10
 
10
- System.out.println(maxSum);
11
+ System.out.println(maxIndex);
11
12
  ```