数组基础与遍历
理解数组基础:固定大小、从零开始的索引、创建与初始化,以及如何安全地遍历数组。
数组基础与遍历 是 CoddyKit 上的免费 Java Academy 课时。 这是第 1 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Java Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Java Academy 课程共包含 3 节课。
什么是数组
数组是由相同类型元素组成的固定大小序列,所有元素使用同一个名称存储,并从 0 开始按索引访问。
- 在您知道元素数量时使用
- 通过索引访问:第一个元素的索引是 0
- 类型安全:所有项目的类型都相同
声明与创建
声明并创建数组:
public class Main {
public static void main(String[] args) {
// Way 1: Declare an array with fixed size
int[] a = new int[3];
// All elements are 0 by default: [0, 0, 0]
// Way 2: Declare and initialize in one line
int[] b = {10, 20, 30};
// This array already has values: [10, 20, 30]
// Print both arrays
System.out.print("Array a: ");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(); // new line
System.out.print("Array b: ");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
}
}
访问与更新
通过索引访问和更新:
对于包含 3 个元素的数组,有效索引为:0、1、2。
public class Main {
public static void main(String[] args) {
// Create and initialize an array
int[] a = {5, 7, 9}; // array has 3 elements: [5, 7, 9]
// Access the first element (index 0)
int first = a[0]; // first = 5
System.out.println("First element: " + first);
// Update the second element (index 1)
a[1] = 8; // now array is [5, 8, 9]
// Print the whole array to see the update
System.out.print("Updated array: ");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
长度与计数循环
使用 a.length 获取元素数量。使用计数循环进行迭代:
public class Main {
public static void main(String[] args) {
// Example array
int[] a = {5, 8, 9}; // 3 elements
int sum = 0; // start with 0
// Loop through each element in the array
for (int i = 0; i < a.length; i = i + 1) {
// Add the current element to sum
sum = sum + a[i];
}
// Print the final result
System.out.println("Sum of array elements = " + sum);
}
}
增强型循环
增强型循环无需索引即可读取元素:
int total = 0;
for (int v : b) {
total = total + v;
}
在不需要索引时使用。
数组演示
运行它:演示会使用计数循环和增强型循环,打印长度、总和、最大值以及每个元素。
public class Main {
static int sum(int[] arr) {
int s = 0;
for (int i = 0; i < arr.length; i = i + 1) {
s = s + arr[i];
}
return s;
}
static int max(int[] arr) {
int m = arr[0];
for (int i = 1; i < arr.length; i = i + 1) {
if (arr[i] > m) m = arr[i];
}
return m;
}
public static void main(String[] args) {
int[] nums = {3, 1, 4, 1, 5};
System.out.println("Length = " + nums.length);
System.out.println("Sum = " + sum(nums));
System.out.println("Max = " + max(nums));
System.out.println("Elements:");
for (int v : nums) {
System.out.println(v);
}
}
}
数组长度
快速检查:如何获取数组 a 中的元素数量?
回顾与下一步
回顾:您创建了数组,访问并更新了项目,使用了 length,并通过计数循环和增强型循环进行迭代。
下一步:学习搜索和反转等常见数组算法。
常见问题解答
「数组基础与遍历」课时是免费的吗?
是的 — 「数组基础与遍历」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Java Academy 课程的其余内容,请升级到 CoddyKit PRO。 Java Academy 课程共包含 3 节课。
「数组基础与遍历」这节课中我会学到什么?
理解数组基础:固定大小、从零开始的索引、创建与初始化,以及如何安全地遍历数组。 你通过在浏览器中直接运行的动手代码来练习 Java Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Java Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Java Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 3 节。
「数组基础与遍历」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Java Academy 课中编写并运行代码吗?
能。每节 Java Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 数组基础与遍历
- 搜索、最小值/最大值与反转
- 按索引插入、删除与移动