Xii's Blog
AP CSA 复习整理

5.7更新
修正"修改指定位置的元素"部分的错误

整理了一下 2020 AP CSA 考试剩下范围内的常用方法等
并且附带了示例代码和注释
欢迎评论区或者私信我 补充 / 挑错 喔!

强制数据类型转换

(type)variableName

int i = 3;
double d = 5.6;
i = (int)d;
// i = 5

常用 Math 类方法

计算绝对值

Math.abs()

int result = Math.abs(-57);
// result = 34

计算平方根

Math.sqrt()

double result = Math.sqrt(64);
// result = 8.0

乘幂运算

Math.pow()


result = Math.pow(2,3);
// result = 8

随机数

生成 [0.0, 1) 范围的随机数

Math.random()

double result = Math.random();
// 0.0 <= result < 1.0

数组Array

创建数组

type[] arrayName = new type[size]

int[] nums = new int[8];
boolean[] flags = new boolean[10];
// 以此类推,或使用以下方法:
int[] nums2 = {1, 2, 3, 4, 5, 6, 7, 8};

获取数组长度

int arrayName.length

int[] nums = new int[8];

int len = nums.length;
// len = 8

字符串类 String

创建字符串

String name = "Bill";
String city = new String("Beijing");

获取字符串长度

int StringName.length()

String school = "BNDS";

int len = school.length();
// len = 4

搜索子串

int StringName.indexOf(String subString)

String school = "Beijing National Day School";

int index = school.indexOf("Nat");
// 返回"Nat"所在下标
// index = 8

int index2 = school.indexOf("o");
// 若出现多个匹配的子串,返回第一个
// index2 = 12

int index3 = school.indexOf("start");
// 若无匹配的子串,返回-1
// index3 = -1

截取子串

String StringName.indexOf(int startIndex)

String StringName.indexOf(int startIndex, int endIndex)

String school = "Beijing National Day School";

String type = school.substring(21);
// 从下标为startIndex开始的字符截取到字符串结尾
// type = "School"

String type = school.substring(8,20);
// 从下标为startIndex开始的字符截取到下标为endIndex - 1
// festival = "National Day"

比较字符串

boolean StringName.equals(String s)

String name = "Bill";

boolean flag = name.equals("Bill");
// flag = true

boolean flag2 = name.equals("bill");
// flag2 = false

 int str1.compareTo(String str2)
比较字符串 str1str2 的大小。若 str1 > str2 ,返回一个大于 0 的整数,否则返回小于 0 的整数,若相等返回 0

String name = "Bill";

int flag = name.compareTo("Aill");
// "Aill" < "Bill"
// flag < 0

int flag2 = name.compareTo("Bill");
// "Bill" = "Bill"
// flag2 = 0

int flag2 = name.compareTo("Cill");
// "Cill" > "Bill"
// flag2 > 0

字符转义符

转义.PNG


ArrayList

创建ArrayList

ArrayList<type> ListName = new ArrayList<type>()

ArrayList<Student> studentList = new ArrayList<Student>;
// 创建了一个空的Person类型ArrayList

添加元素

ListName.add(Object e)

ListName.add(Object e, int index)

ArrayList<Student> studentList = new ArrayList<Student>;
// 创建了一个空的Person类型ArrayList
Student s1 = new Student("Bill", 17);
Student s2 = new Student("Bill", 17);
// 创建Student对象

studentList.add(s1);
// 向studentList内添加一个元素"s1"

studentList.add(s2, 0);
// 向studentList的第0个index插入一个元素"s2"

删除元素

ListName.remove(int index)

ArrayList<Student> studentList = new ArrayList<Student>;
Student s1 = new Student("Bill", 17);
Student s2 = new Student("Bill", 17);
studentList.add(s1);
studentList.add(s2);

studentList.remove(0);
// 删除studentList的第0个元素

获取元素

ListName.get(int index)

ArrayList<Student> studentList = new ArrayList<Student>;
Student s1 = new Student("Bill", 17);
Student s2 = new Student("Bill", 17);
studentList.add(s1);
studentList.add(s2);

studentList.get(0);
// 获取studentList的第0个元素

修改指定位置的元素

ListName.set(int index, Object e)

ArrayList<Student> studentList = new ArrayList<Student>;
Student s1 = new Student("Bill", 17);
Student s2 = new Student("Bill", 17);
studentList.add(s1);
studentList.add(s2);

studentList.set(0, s2);
// 将studentList的第0个元素设置为s2