slug
javareference2
type
Post
status
Published
category
AP CSA
date
Mar 3, 2024
summary
tags
notes
password
icon
AP Computer Science A
Classroom Handout
Java Quick Reference 核心讲义
Topic
Using the AP CSA Java Quick Reference Effectively
Course
AP Computer Science A
Language Support
Chinese explanation + English code
1. Learning Objectives
By the end of this lesson, students should be able to:
- understand the purpose of the major classes listed in the AP CSA Java Quick Reference
- explain what each important method does
- trace code involving strings, math functions, lists, and file input
- write short AP-style code using these methods correctly
- avoid common mistakes in exam questions
这份 Quick Reference 不是完整 Java 语法书,而是 AP CSA 考试中常见标准库方法的“官方速查范围”。考试题经常围绕这些方法出题,因此你不仅要认识它们,还要会判断输出、会读代码、会写简单应用。
2. Warm-up
在 AP CSA 中,很多题并不考“你会不会写很长的程序”,而是考你:
- 能不能快速判断一个方法的作用
- 能不能根据索引、返回值、参数判断程序输出
- 能不能把几个常用方法组合起来解决问题
所以这份 handout 的重点不是“背定义”,而是“真正会用”。
3. Section A — String Class
上传材料中的
String 部分包含这些核心内容:构造器、length()、两个版本的 substring()、indexOf()、equals()、compareTo()、split()。3.1 String(String str)
What it does
Creates a new
String object that has the same characters as str.中文理解
根据已有字符串,再创建一个新的字符串对象。
Example
Teacher note
考试里更常见的仍然是:
但你要知道构造器在 quick reference 里确实存在。
3.2 length()
What it does
Returns the number of characters in a
String.中文理解
返回字符串长度。
Example
Output
Key idea
- 长度是字符总数
- 索引从
0开始
- 最后一个字符索引 =
length() - 1
3.3 substring(int from, int to)
What it does
Returns the substring beginning at index
from and ending at index to - 1.中文理解
从
from 开始取,到 to-1 结束,右边不包含。Example
Output
Exam trap
很多学生会误以为取到索引
5。实际上不会,右端不包含。3.4 substring(int from)
What it does
Returns
substring(from, length()).中文理解
从某个位置一直取到最后。
Example
Output
3.5 indexOf(String str)
What it does
Returns the index of the first occurrence of
str; returns -1 if not found.中文理解
找子串第一次出现的位置;如果找不到,返回
-1。Example
Output
Common use
- 判断某内容是否出现
- 搜索关键词
- 配合
substring()做字符串处理
3.6 equals(Object other)
What it does
Returns
true if the string has the same sequence of characters as other; otherwise returns false.中文理解
比较两个字符串内容是否一样。
Example
Output
Important reminder
比较字符串内容,优先用
equals()。3.7 compareTo(String other)
What it does
Returns:
- a value
< 0if this string is less thanother
0if equal
- a value
> 0if greater thanother
Strings are ordered alphabetically.
中文理解
按字典序比较字符串大小。
Example
Meaning
- negative
- 0
- positive
Teacher tip
考试通常不要求你记精确数字,只要求知道正、负、零分别代表什么。
3.8 split(String del)
What it does
Returns a
String[] after splitting the string around the given delimiter.中文理解
按分隔符切割字符串,结果是字符串数组。
Example
4. Section B — Integer and Double
材料中给出的内容包括
Integer.MIN_VALUE、Integer.MAX_VALUE、Integer.parseInt(String s) 和 Double.parseDouble(String s)。4.1 Integer.MIN_VALUE and Integer.MAX_VALUE
What they do
They represent the minimum and maximum values of an
int.中文理解
表示
int 能表示的最小值和最大值。Example
Exam use
常用于找最大值、最小值问题中的初始化。
Example
4.2 Integer.parseInt(String s)
What it does
Returns the string argument as an
int.中文理解
把字符串转成整数。
Example
Output
4.3 Double.parseDouble(String s)
What it does
Returns the string argument as a
double.中文理解
把字符串转成小数。
Example
5. Section C — Math Class
材料中列出
abs()、pow()、sqrt()、random()。5.1 Math.abs()
What it does
Returns the absolute value of an
int or double.Example
5.2 Math.pow(double base, double exponent)
What it does
Returns the value of the first parameter raised to the power of the second parameter.
Example
Output
Reminder
返回值是
double。5.3 Math.sqrt(double x)
What it does
Returns the nonnegative square root of a
double.Example
5.4 Math.random()
What it does
Returns a
double value greater than or equal to 0.0 and less than 1.0.中文理解
返回
[0.0, 1.0) 范围内的随机小数。Example
AP-style example
Generate an integer from 1 to 6:
Why it works
Math.random()gives[0, 1)
- multiply by
6gives[0, 6)
- cast to
intgives0,1,2,3,4,5
- add
1gives1,2,3,4,5,6
6. Section D — ArrayList Class
材料中列出
size()、两种 add()、get()、set()、remove()。6.1 size()
What it does
Returns the number of elements in the list.
Example
6.2 add(E obj)
What it does
Appends an object to the end of the list.
Example
6.3 add(int index, E obj)
What it does
Inserts an object at a certain position and shifts later elements to the right.
Example
Result
6.4 get(int index)
What it does
Returns the element at the specified index.
Example
6.5 set(int index, E obj)
What it does
Replaces the element at an index and returns the old element.
Example
6.6 remove(int index)
What it does
Removes the element at an index, shifts later elements left, and returns the removed element.
Example
7. Section E — File and Scanner
材料中给出
File(String pathname),以及 Scanner 的 nextInt()、nextDouble()、nextBoolean()、nextLine()、next()、hasNext()、close()。7.1 File(String pathname)
What it does
Constructs a
File object using a string path.Example
7.2 Scanner(File f)
What it does
Creates a scanner that reads from a file.
Example
7.3 nextInt(), nextDouble(), nextBoolean()
What they do
Read the next value of the corresponding type from the input source. If the expected value does not exist, an
InputMismatchException can occur.Example
7.4 nextLine()
What it does
Reads the next whole line as a string. The reference also warns that it may return an empty string if called immediately after another
Scanner method.Example
Very important exam trap
这里
line 可能是空字符串,因为 nextInt() 没有读掉行末换行,紧接着 nextLine() 把那一个换行读走了。这个陷阱在 AP CSA 中很常见,而且你的上传材料里明确提醒了这一点。7.5 next()
What it does
Reads the next string token.
Example
Difference from nextLine()
next()读一个词
nextLine()读一整行
7.6 hasNext()
What it does
Returns
true if there is another item to read.Example
7.7 close()
What it does
Closes the scanner.
Example
8. Section F — Object Class
材料最后还列出
equals(Object other) 和 toString()。8.1 equals(Object other)
中文理解
比较两个对象是否相等。
对于
String,equals() 代表比较内容是否一致。上传材料在 String 和 Object 两处都提到了这个方法。8.2 toString()
中文理解
返回对象的字符串形式。
Example
9. Model Examples
Example 1 — Count a letter in a string
Why this is useful
它综合使用了
length()、substring()、equals(),都是 quick reference 中最常考的方法。Example 2 — Find the maximum in an ArrayList
Why this is useful
它综合使用了
Integer.MIN_VALUE、size()、get()、add()。Example 3 — Read integers from a file and compute the sum
Why this is useful
它综合使用了
File、Scanner(File)、hasNext()、nextInt()、close()。10. Check for Understanding
Question 1
What does this print?
Question 2
What does this print?
Question 3
What does this print?
Question 4
What does this print?
Question 5
What values can this expression produce?
Question 6
After this code runs, what is in the list?
Question 7
What is returned by
remove(1) here, and what remains in the list?Question 8
Why might
line be empty?Question 9
What does this print?
Question 10
What does this print?
Question 11
What does this print?
Question 12
What does this print?
Question 13
What does this print?
Question 14
What does this print?
Question 15
What does this print?
Question 16
What does this print?
Question 17
What does this print?
Question 18
What values can this expression produce?
Question 19
After this code runs, what is the value of
list.size()?Question 20
What does this print?
Question 21
After this code runs, what is stored in
old, and what is in list?Question 22
What does this code do?
Question 23
What does this print?
Question 24
What does this print?
Question 25
What does this print?
如果你要,我也可以把这 25 题对应的 Answers 部分一起直接补好。
11. Answers
Answers
Answer 1
6Answer 2
omp因为
substring(1, 4) 取的是索引 1, 2, 3,不包含索引 4。Answer 3
2因为
"na" 在 "banana" 中第一次出现的位置是索引 2。Answer 4
true因为两个字符串内容相同,
equals() 比较的是内容。Answer 5
Possible values:
0, 1, 2, 3因为
Math.random() 产生 [0.0, 1.0) 之间的值,乘以 4 后范围是 [0.0, 4.0),转成 int 后只能是 0 到 3。Answer 6
[red, blue, green]因为
add(1, "blue") 会在索引 1 插入 "blue",原来后面的元素右移。Answer 7
removed = 2remaining list:
[1, 3]因为
remove(1) 删除索引 1 处的元素,也就是 2,后面的元素左移。Answer 8
因为
nextInt() 读取整数后,不会读走这一行末尾的换行符。紧接着 nextLine() 会把这个换行直接读掉,所以 line 可能是空字符串。Answer 9
phant因为从索引
3 开始一直取到字符串结尾。Answer 10
1
因为
"hello" 中找不到 "z",所以 indexOf() 返回 -1。Answer 11
true因为
"dog" 在字典序中大于 "cat",所以 compareTo() 的结果大于 0。Answer 12
3因为按逗号分割后得到:
"red"
"blue"
"green"
共 3 个元素。
Answer 13
255因为先把
"250" 转成整数 250,再加 5。Answer 14
5.0因为
"2.5" 被转成 double 值 2.5,乘以 2 得到 5.0。Answer 15
12因为绝对值就是去掉负号后的非负数值。
Answer 16
9.0因为
3² = 9,Math.pow() 返回类型是 double,所以输出 9.0。Answer 17
7.0因为
49 的平方根是 7,返回值类型是 double。Answer 18
Possible values:
1, 2, 3, 4, 5, 6因为:
Math.random()gives[0, 1)
- multiply by
6gives[0, 6)
- cast to
intgives0, 1, 2, 3, 4, 5
- add
1gives1, 2, 3, 4, 5, 6
Answer 19
3因为列表里加入了三个元素:
"A"、"B"、"C"。Answer 20
10因为索引
2 对应第三个元素,而列表内容是 [5, 8, 10]。Answer 21
old = "dog"list = [cat, bird]因为
set(1, "bird") 会把索引 1 的旧值 "dog" 替换掉,并返回旧值。Answer 22
It repeatedly reads and prints the next token from the input source until there is no more input.
中文解释:
这段代码会不断检查是否还有下一个输入项;如果有,就用
next() 读取一个 token 并输出,直到没有更多内容为止。Answer 23
a因为
substring(0, 1) 只取索引 0 这一位字符。Answer 24
nana因为
substring(2, 6) 取索引 2, 3, 4, 5,对应字符是:- 2 →
n
- 3 →
a
- 4 →
n
- 5 →
a
Answer 25
First line printed:
xSecond line printed:
[y, z]因为
remove(0) 删除并返回索引 0 处的元素 "x",删除后列表只剩 [y, z]。12. Summary Box
今天这份 handout 最重要的考试高频点有:
String.length()
String.substring()
String.equals()
String.indexOf()
Integer.parseInt()
Double.parseDouble()
Math.random()
ArrayList.add() / get() / set() / remove() / size()
Scanner.nextInt() / next() / nextLine() / hasNext()
nextLine()afternextInt()的空行陷阱
- 作者:现代数学启蒙
- 链接:https://www.math1234567.com/article/javareference2
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
相关文章






