Last active
December 19, 2015 22:59
-
-
Save shikajiro/6031383 to your computer and use it in GitHub Desktop.
Java APIのArraysには、binarySearch というメソッドがあります。
これは、「配列内の値を検索して、見つかった添字を返す」という機能を持っています。
※添字とは randoms[5] を例にした場合の 5 の部分です。
これを使って、以下のソースコードを参考に、配列の中から 11 が格納されている添字を探すプログラムを書いてください。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class HomeWork { | |
public static void main(String[] args) { | |
int[] numbers = {43, 23, 45, 11, 10982}; | |
int index = 0; | |
//検索する前に必ずソート(昇順で並び替え)しておく必要がある。 | |
Arrays.sort(numbers); | |
//TODO ここに添字を検索する処理を書き、indexに結果を代入する。 | |
System.out.println("検索した添字は"+index+"でした"); | |
//23が格納されている配列を100で上書きする。 | |
numbers[index] = 100; | |
for (int i : numbers) { | |
System.out.println(i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment