第三周学习概览

本周我们完成了从基础语法到程序结构的跨越,掌握了数组存储方法复用递归思想三大核心能力。

数组操作 4天

一维数组、二维数组、Arrays工具类

方法设计 3天

方法定义、参数传递、重载、可变参数

递归思想 2天

递归调用、终止条件、栈理解

数组知识完全总结

从声明到操作,从基础到高级

一维数组要点

// 三种声明方式 int[] arr1 = new int[5]; // 默认值0 int[] arr2 = {1, 2, 3, 4, 5}; // 静态初始化 int[] arr3 = new int[]{1,2,3}; // 匿名数组
最佳实践:使用arr.length获取长度,避免越界

二维数组操作

// 不规则二维数组 int[][] triangle = { {1}, {2, 3}, {4, 5, 6} }; // 动态创建 int[][] matrix = new int[3][4]; // 3行4列

Arrays工具类

  • Arrays.sort(arr) - 快速排序
  • Arrays.binarySearch(arr, key) - 二分查找
  • Arrays.fill(arr, value) - 填充数组
  • Arrays.toString(arr) - 数组转字符串
  • Arrays.copyOf(arr, length) - 数组复制
传统遍历
for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }
增强for循环
for (int value : arr) { System.out.println(value); }

方法设计完整指南

从定义到优化,掌握可复用代码设计

1

方法定义模板

[访问修饰符] [static] 返回类型 方法名([参数列表]) { // 方法体 [return 返回值;] }
2

参数传递机制

public class ParameterDemo { public static void main(String[] args) { int x = 10; modifyPrimitive(x); // x仍为10 int[] arr = {1, 2, 3}; modifyArray(arr); // arr内容被修改 } public static void modifyPrimitive(int num) { num = 100; // 不影响原值 } public static void modifyArray(int[] array) { array[0] = 999; // 影响原数组 } }

方法重载规则

// 同名不同参 public static int add(int a, int b) { return a + b; } public static double add(double a, double b) { return a + b; } public static int add(int a, int b, int c) { return a + b + c; }

可变参数

public static int sum(int... numbers) { int total = 0; for (int num : numbers) { total += num; } return total; } // 调用:sum(1,2,3) 或 sum(1,2,3,4,5)

递归思想深度解析

从恐惧到掌握,理解递归的本质

递归三要素口诀

  1. 终止条件:明确何时停止
  2. 递归调用:调用自身解决子问题
  3. 结果合并:将子结果组合成最终答案
阶乘递归
public static long factorial(int n) { if (n <= 1) return 1; // 终止 return n * factorial(n-1); // 递归 }
斐波那契递归
public static int fibonacci(int n) { if (n <= 2) return 1; // 终止 return fibonacci(n-1) + fibonacci(n-2); }

递归调用栈可视化

// 执行factorial(4)的调用栈 factorial(4) → 4 * 6 = 24 └─ factorial(3) → 3 * 2 = 6 └─ factorial(2) → 2 * 1 = 2 └─ factorial(1) → 1

本周常见错误汇总

踩过的坑,不要再踩第二次

数组相关错误

  • ArrayIndexOutOfBoundsException:索引越界,记住索引从0开始,最大是length-1
  • NullPointerException:未初始化数组就使用
  • 二维数组长度误解:matrix.length是行数,matrix[0].length是列数
// 错误示例 int[] arr = new int[5]; System.out.println(arr[5]); // 越界!

方法相关错误

  • 返回值类型不匹配:声明int却返回String
  • 参数传递误解:误以为基本类型能修改原值
  • 递归无终止:导致StackOverflowError

调试技巧

  • 使用IDE调试器的单步执行功能
  • 在递归方法中添加深度参数追踪调用层次
  • 使用Arrays.toString()快速查看数组内容
  • 为方法添加详细的JavaDoc注释

综合实战项目

学生管理系统V2.0 - 数组+方法版本

项目功能

  • 使用数组存储学生信息(姓名、成绩)
  • 方法实现增删改查操作
  • 递归实现成绩排名算法
  • Arrays工具类进行排序
public class StudentManager { private static String[] names = new String[100]; private static double[] scores = new double[100]; private static int count = 0; public static void main(String[] args) { // 添加学生 addStudent("张三", 85.5); addStudent("李四", 92.0); // 排序并显示 sortByScore(); displayStudents(); } public static void addStudent(String name, double score) { if (count >= names.length) { System.out.println("数组已满!"); return; } names[count] = name; scores[count] = score; count++; } public static void sortByScore() { // 使用冒泡排序算法 for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - i - 1; j++) { if (scores[j] < scores[j+1]) { // 交换成绩和姓名 double tempScore = scores[j]; scores[j] = scores[j+1]; scores[j+1] = tempScore; String tempName = names[j]; names[j] = names[j+1]; names[j+1] = tempName; } } } } public static void displayStudents() { System.out.println("=== 学生成绩排名 ==="); for (int i = 0; i < count; i++) { System.out.printf("第%d名:%s - %.1f分%n", i+1, names[i], scores[i]); } } }

掌握程度检查清单

逐项检查,确保真正掌握

  • 能独立声明、初始化并使用一维数组
  • 掌握二维数组的创建和遍历
  • 熟练使用Arrays工具类的常用方法
  • 能正确定义带参数和返回值的方法
  • 理解基本类型和引用类型的参数传递区别
  • 能使用方法重载提高代码可读性
  • 掌握可变参数的使用场景
  • 能编写简单的递归方法(如阶乘、斐波那契)
  • 理解递归调用栈的工作原理
  • 能调试递归方法的执行过程

下周预告

第四周我们将进入面向对象编程的世界,学习类和对象、封装、继承和多态等核心概念。准备好迎接编程思想的重大飞跃!

提前预习第四周内容