slug
javareference2
type
Post
status
Published
category
8-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 的重点不是“背定义”,而是“真正会用”。
提醒:
不含
public class Main 的代码当成程序片段用jshell来执行;含有
public class Main 的代码当成完整程序执行。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
- ?
- ?
- ?
Teacher tip
考试通常不要求你记精确数字,只要求知道正、负、零分别代表什么。
3.8 split(String del)
What it does
Returns a
String[] after splitting the string around the given delimiter.中文理解
按分隔符切割字符串,结果是字符串数组。
Example
Output
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
Output
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
Output
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
Output
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
Output
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
Output
6.5 set(int index, E obj)
What it does
Replaces the element at an index and returns the old element.
Example
Output
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 中很常见,而且你的上传材料里明确提醒了这一点。trap的完整解说版
当然。下面给你一个完整、可运行、能清楚暴露这个陷阱的例子。
例子 1:错误示范
假设输入是:
你本来可能以为输出会是:
但实际上很可能是:
为什么会这样?
这一行:
只读走了
18,没有读走你按下回车产生的换行符。所以输入缓冲区里其实还剩一个:
接着这一行:
会直接把这个“上一行残留的换行”读掉,于是得到的就是一个空字符串
""。所以
name 变成了空的。输入过程的本质
假设你输入:
缓冲区大致可以理解成:
nextInt()读走了:
还剩下:
- 第一次
nextLine()读走的其实只是这一整行剩余部分,也就是:
因为这一行除了换行什么都没有了。
正确写法
解决方法是:在
nextInt() 后面,先额外调用一次 nextLine(),把残留换行吃掉。同样输入:
这次输出就是:
再给你一个更贴近考试题的例子
错误版本
如果输入:
输出会是:
因为
comment 是空串。修正版
输入:
输出:
考试里怎么判断这种陷阱
看到这种组合时要立刻警觉:
后面如果紧跟着:
就要想到:
前面的输入方法可能没有吃掉换行,导致
nextLine() 读到空字符串。一句话记忆
可以记成:
nextInt()读数字,nextLine()读整行;数字后面的回车还在,所以整行先读到空。
最后给你一个课堂/考试标准版示例
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() 代表比较内容是否一致,而不是比较它们是不是同一个对象。Example 1:两个字符串内容相同
输出:
解释:
因为
a 和 b 的内容都是 "hello",所以 equals() 返回 true。Example 2:两个字符串内容不同
输出:
解释:
因为两个字符串内容不同,所以返回
false。Example 3:和 == 对比理解
输出:
解释:
==比较的是两个变量是否指向同一个对象
equals()比较的是两个对象的内容是否相同
这里
a 和 b 虽然内容都是 "cat",但它们是两个不同的对象,所以:a == b是false
a.equals(b)是true
一句话总结
对于
String:==看“是不是同一个东西”
equals()看“内容是不是一样”
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
下面是一套 42 道 AP CSA 风格选择题,专门测试你对 Java Quick Reference 的理解与掌握,覆盖
String、Integer / Double、Math、ArrayList、File、Scanner、Object 等核心内容。题目依据你上传的 reference sheet 编写。AP CSA Multiple Choice Practice (42 Questions)
1.
Which method returns the number of characters in a
String?A.
size()B.
length()C.
count()D.
charAt()2.
What does
substring(int from, int to) return?A. Characters from index
from through index toB. Characters from index
from through index to - 1C. Characters from index
from + 1 through index toD. All characters except those from
from to to3.
What does
substring(int from) return?A.
substring(0, from)B.
substring(from, from + 1)C.
substring(from, length())D.
substring(1, from)4.
What does
indexOf(String str) return if str is not found?A.
0B.
1C.
nullD.
-15.
Which statement about
equals(Object other) for String is correct?A. It compares memory addresses only
B. It compares whether two Strings contain the same sequence of characters
C. It always returns
true for nonempty StringsD. It returns an
int6.
What does
compareTo(String other) return when the two Strings are equal?A.
-1B.
1C.
0D.
true7.
If
compareTo returns a value greater than 0, what does that mean?A. This String is shorter than
otherB. This String is alphabetically greater than
otherC. This String is equal to
otherD. This String is not a valid String
8.
Which method returns a
String[]?A.
substring()B.
indexOf()C.
split()D.
compareTo()9.
Which class contains the constant
MIN_VALUE relevant to int?A.
MathB.
IntegerC.
DoubleD.
Object10.
What does
Integer.parseInt(String s) return?A. A
doubleB. A
StringC. An
intD. A
boolean11.
What does
Double.parseDouble(String s) return?A. A
doubleB. An
intC. A
String[]D. A
File12.
Which method returns the absolute value of an
int?A.
Math.pow(int)B.
Math.abs(int)C.
Math.sqrt(int)D.
Integer.abs(int)13.
Which method computes a base raised to an exponent?
A.
Math.random(base, exponent)B.
Math.sqrt(base, exponent)C.
Math.pow(base, exponent)D.
Math.abs(base, exponent)14.
What does
Math.sqrt(double x) return?A. Any square root, positive or negative
B. The nonnegative square root
C. The cube root
D. An
int15.
Which statement about
Math.random() is correct?A. It returns an
int from 0 to 1B. It returns a
double greater than 0.0 and less than or equal to 1.0C. It returns a
double greater than or equal to 0.0 and less than 1.0D. It returns either
0 or 116.
What is the return type of
ArrayList.size()?A.
booleanB.
intC.
doubleD.
E17.
What does
add(E obj) do in an ArrayList?A. Inserts
obj at the beginning onlyB. Appends
obj to the end and returns trueC. Replaces the last element and returns it
D. Removes the last element
18.
What does
add(int index, E obj) do?A. Replaces the element at
indexB. Inserts
obj at index, shifting later elements rightC. Deletes the element at
indexD. Returns the old element at
index19.
What does
get(int index) return?A. The size of the list
B. Whether the index exists
C. The element at position
indexD. The last element only
20.
What does
set(int index, E obj) return?A.
true if replacement succeedsB. The new element
C. The old element formerly at that index
D. Nothing
21.
What does
remove(int index) return?A. The removed element
B.
true if removal succeedsC. The new size
D. Nothing
22.
After
remove(int index), what happens to elements after that index?A. They disappear permanently
B. They shift to the right
C. They shift to the left
D. They stay in the same place
23.
For
add(int index, E obj), which condition on index is stated in the reference sheet?A.
0 < index < sizeB.
0 <= index <= sizeC.
1 <= index < sizeD.
0 <= index < size - 124.
Which constructor creates a
File object?A.
File(int pathname)B.
File(String pathname)C.
File()D.
File(Scanner s)25.
Which constructor creates a
Scanner for reading from a file?A.
Scanner(String s)B.
Scanner(int x)C.
Scanner(File f)D.
Scanner(boolean b)26.
What does
Scanner.nextInt() return?A. The next line as a
StringB. The next
int read from the input sourceC. The next
doubleD. The number of remaining tokens
27.
According to the sheet,
nextInt() may result in an InputMismatchException when:A. The file is empty only
B. The next
int does not exist or is out of rangeC. The scanner has been closed only
D. The next token is a
String with spaces28.
What does
Scanner.nextDouble() return?A. The next
doubleB. The next
int converted automaticallyC. A
StringD. A
boolean29.
What does
Scanner.nextBoolean() return?A. The next line
B. A random boolean
C. The next boolean read from the input source
D. Whether the file exists
30.
Which Scanner method can return the empty string if called immediately after another Scanner method reading from the source?
A.
next()B.
nextInt()C.
nextLine()D.
hasNext()31.
What does
Scanner.next() return?A. The next
String read from the input sourceB. The rest of the entire file
C. The next character
D. The next line including line break
32.
What does
hasNext() return?A. The next token as a
StringB.
true if there is another item to read, false otherwiseC. The number of items left
D.
true only for files, not keyboard input33.
What does
close() do for a Scanner?A. Deletes the file
B. Resets the scanner to the beginning
C. Closes the scanner
D. Returns the final token
34.
Which methods are listed under
Object Class in the sheet?A.
hashCode() and clone()B.
equals(Object other) and toString()C.
compareTo() and toString()D.
length() and equals()35.
Which of the following is a correct pairing of method and return type?
A.
indexOf → booleanB.
equals → intC.
compareTo → intD.
split → double[]36.
Which method returns a
boolean?A.
String.length()B.
String.equals(Object other)C.
String.indexOf(String str)D.
ArrayList.size()37.
Which method returns an
int?A.
Math.random()B.
Double.parseDouble()C.
String.indexOf(String str)D.
String.substring(int from)38.
Which method returns a
double?A.
Math.sqrt(double x)B.
Integer.parseInt(String s)C.
ArrayList.size()D.
Scanner.nextInt()39.
Which method definitely modifies an existing
ArrayList element rather than inserting or removing one?A.
get(int index)B.
set(int index, E obj)C.
size()D.
remove(int index)40.
Which statement is supported by the reference sheet?
A.
split returns an ArrayList<String>B.
random returns values from 1.0 to 10.0C.
compareTo orders Strings based upon the alphabetD.
nextLine() always skips empty lines41.
A student wants to convert the text
"123" into a numeric primitive int. Which method should be used?A.
Double.parseDouble("123")B.
Integer.parseInt("123")C.
Math.abs("123")D.
String.indexOf("123")42.
A student wants to read from a file named
"data.txt". Which line correctly creates the File object described in the reference sheet?A.
Scanner f = new Scanner("data.txt");B.
File f = new File("data.txt");C.
File f = new File();D.
String f = new File("data.txt");11. Answers
Answers
- B
- B
- C
- D
- B
- C
- B
- C
- B
- C
- A
- B
- C
- B
- C
- B
- B
- B
- C
- C
- A
- C
- B
- B
- C
- B
- B
- A
- C
- C
- A
- B
- C
- B
- C
- B
- C
- A
- B
- C
- B
- B
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 许可协议,转载请注明出处。
相关文章








