検索、最小値・最大値、反転
線形探索を実装し、1回の走査で最小値と最大値を求め、配列をその場で、またはコピーして反転します。
「検索、最小値・最大値、反転」はCoddyKit上の無料Java Academyレッスンです。 これはレッスン2/3です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJava Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Java Academyコースには全3レッスンが含まれています。
線形探索
線形探索では、対象を見つけるまで各要素を順番に確認します。
- 見つかったらインデックスを返します
- 見つからなければ
-1を返します breakを使って早めに停止します
public class Main {
public static void main(String[] args) {
// Example array
int[] a = {5, 8, 9, 12, 15};
int t = 9; // target value to search
int idx = -1; // will stay -1 if not found
// Loop through each index of the array
for (int i = 0; i < a.length; i = i + 1) {
// Check if current element matches the target
if (a[i] == t) {
idx = i; // store the index where found
break; // exit loop early (found the target)
}
}
// Print result
if (idx != -1) {
System.out.println("Found " + t + " at index " + idx);
} else {
System.out.println(t + " not found in array");
}
}
}
findIndex()
探索をメソッドにカプセル化します:
public class Main {
public static void main(String[] args) {
// Example array
int[] a = {5, 8, 9, 12, 15};
int t = 9; // target value we are searching for
int idx = -1; // default index (-1 means "not found")
// Loop through each index of the array
for (int i = 0; i < a.length; i = i + 1) {
// Check if current element matches the target
if (a[i] == t) {
idx = i; // store the index where it was found
break; // stop searching (first match found)
}
}
// Print result
if (idx != -1) {
System.out.println("Found " + t + " at index " + idx);
} else {
System.out.println(t + " not found in array");
}
}
}
最小値/最大値のワンパス処理
1回の走査で最小値と最大値を計算します:
public class Main {
public static void main(String[] args) {
// Example array
int[] a = {5, 8, 2, 12, 15, 1};
// Initialize min and max with the first element
int min = a[0];
int max = a[0];
// Loop starts from the second element (index 1)
for (int i = 1; i < a.length; i = i + 1) {
// If current element is smaller than current min → update min
if (a[i] < min) min = a[i];
// If current element is larger than current max → update max
if (a[i] > max) max = a[i];
}
// Print results
System.out.println("Minimum value = " + min);
System.out.println("Maximum value = " + max);
}
}
インプレース反転
両端を入れ替えながら内側へ進み、インプレースで反転します:
public class Main {
public static void main(String[] args) {
// Example array
int[] a = {5, 8, 9, 12, 15};
int i = 0; // start pointer (left side)
int j = a.length - 1; // end pointer (right side)
// Keep swapping until the two pointers meet
while (i < j) {
// Swap elements at positions i and j
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
// Move pointers toward the center
i = i + 1;
j = j - 1;
}
// Print the reversed array
System.out.print("Reversed array: ");
for (int k = 0; k < a.length; k++) {
System.out.print(a[k] + " ");
}
}
}
反転コピー
反転コピーを作成します(元の配列は変更しません):
public class Main {
public static void main(String[] args) {
// Example array
int[] a = {5, 8, 9, 12, 15};
// Create a new array b with the same length
int[] b = new int[a.length];
// Copy elements from a into b in reverse order
for (int i = 0; i < a.length; i = i + 1) {
// (a.length - 1 - i) gives the reversed index
b[a.length - 1 - i] = a[i];
}
// Print original array
System.out.print("Original array: ");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(); // newline
// Print reversed array
System.out.print("Reversed array: ");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
}
}
探索/最小値最大値/反転のデモ
実行してみましょう: 探索結果、最小値/最大値、反転した配列を確認します。
public class Main {
static int findIndex(int[] a, int t) {
for (int i = 0; i < a.length; i = i + 1) {
if (a[i] == t) return i;
}
return -1;
}
static int min(int[] a) {
int m = a[0];
for (int i = 1; i < a.length; i = i + 1) {
if (a[i] < m) m = a[i];
}
return m;
}
static int max(int[] a) {
int m = a[0];
for (int i = 1; i < a.length; i = i + 1) {
if (a[i] > m) m = a[i];
}
return m;
}
static void reverseInPlace(int[] a) {
int i = 0, j = a.length - 1;
while (i < j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
i = i + 1;
j = j - 1;
}
}
static int[] reversedCopy(int[] a) {
int[] b = new int[a.length];
for (int i = 0; i < a.length; i = i + 1) {
b[a.length - 1 - i] = a[i];
}
return b;
}
static String join(int[] a) {
String s = "";
for (int i = 0; i < a.length; i = i + 1) {
s = s + a[i] + (i + 1 < a.length ? " " : "");
}
return s;
}
public static void main(String[] args) {
int[] a = {3, 1, 4, 1, 5};
System.out.println("findIndex 4 -> " + findIndex(a, 4));
System.out.println("findIndex 9 -> " + findIndex(a, 9));
System.out.println("min = " + min(a));
System.out.println("max = " + max(a));
int[] b = reversedCopy(a);
System.out.println("reversedCopy: " + join(b));
reverseInPlace(a);
System.out.println("reverseInPlace: " + join(a));
}
}
線形探索の確認
クイックチェック: a内のtのインデックス、または-1を返すスニペットはどれですか?
まとめと次のステップ
まとめ: 線形探索、最小値/最大値、配列を反転する2つの方法を実装しました。
次のステップ: 挿入、削除(インデックス指定)、要素のシフトを学びます。
よくある質問
「検索、最小値・最大値、反転」レッスンは無料ですか?
はい。「検索、最小値・最大値、反転」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Java Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Java Academyコースには全3レッスンが含まれています。
「検索、最小値・最大値、反転」で何を学びますか?
線形探索を実装し、1回の走査で最小値と最大値を求め、配列をその場で、またはコピーして反転します。 ブラウザで直接実行するハンズオンコードでJava Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Java Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJava Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/3です。
「検索、最小値・最大値、反転」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJava Academyレッスンでコードを書いて実行できますか?
はい。すべてのJava Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 配列の基礎と反復処理
- 検索、最小値・最大値、反転
- インデックスによる挿入・削除とシフト