搜索、最小值/最大值与反转
实现线性搜索,在一次遍历中计算最小值和最大值,并原地或通过复制反转数组。
搜索、最小值/最大值与反转 是 CoddyKit 上的免费 Java Academy 课时。 这是第 2 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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");
}
}
}
单次遍历求最小值和最大值
通过一次遍历计算最小值和最大值:
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] + " ");
}
}
}
搜索/MinMax/反转演示
运行它:查看搜索结果、最小值和最大值,以及反转后的数组。
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?
回顾与下一步
回顾:您实现了线性搜索、最小值和最大值计算,以及两种数组反转方式。
下一步:学习插入、按索引删除以及移动元素。
常见问题解答
「搜索、最小值/最大值与反转」课时是免费的吗?
是的 — 「搜索、最小值/最大值与反转」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Java Academy 课程的其余内容,请升级到 CoddyKit PRO。 Java Academy 课程共包含 3 节课。
「搜索、最小值/最大值与反转」这节课中我会学到什么?
实现线性搜索,在一次遍历中计算最小值和最大值,并原地或通过复制反转数组。 你通过在浏览器中直接运行的动手代码来练习 Java Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Java Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Java Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 3 节。
「搜索、最小值/最大值与反转」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Java Academy 课中编写并运行代码吗?
能。每节 Java Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 数组基础与遍历
- 搜索、最小值/最大值与反转
- 按索引插入、删除与移动