CSA 知识点梳理
-
basic input and output
-
primitive type
-
运算符和表达式
-
语句
-
方法
-
array
-
ArrayList
-
类
-
随机数
-
类
-
继承多态,动态绑定
-
递归
-
writing class
basic input and output
-
-
system.out.println/println
primitive type
-
-
-
(int)Math.random() * 10 结果始终为 0
-
-
-
-
运算符和表达式
运算符
单个值是表达式,表达式有运算符和操作数组成;表达式会被求值有返回值的函数调用也是表达式
短路运算
int a = 3;
int b = 0;
if (a < 2 && a / b == 0)
{
System.out.println("a");
}
else
{
System.out.println("b");
}
b
int a = 3;
int b = 0;
if (a > 2 || a / b == 0)
{
System.out.println("a");
}
else
{
System.out.println("b");
}
a
a / b
---------------------------------------------------------------------------
java.lang.ArithmeticException: / by zero
at .(#20:1)
语句
语句可以组成起来,形成新的命令,就是方法。
方法
形式参数 form arameter
实际参数,actual argument 实际调用方法时候传递的参数
array
只能存储相同一类型的数据,不能增删元素,不能改变大小。
int[] nums = new int[10];
int[] scores = {1, 2, 3, 4};
boolean[] lights = new boolean[3];
String[] colors = new String[10];
// access element
scores[0]
1
// assignment
scores[1] = 10
10
scores.length
4
2D Array
int[][] arr2d = new int[2][3];
int rows = arr2d.length;
int cols = arr2d[0].length;
for (int[] row: arr2d)
{
for(int value: row)
{
// print value
}
}
ArrayList
ArrayList<String> colors = new ArrayList<String>();
colors.add("red")
true
colors
[red]
colors.add(0, "black")
colors
[black, red]
colors.set(1, "blue")
red
colors.size()
2
colors
[black, blue]
colors.set(1, "red")
blue
colors.remove(1)
red
colors.add("red");
colors.add("blue");
colors.add("brown");
colors.add("black");
true
colors
[black, red, blue, brown, black]
for (int i = 0; i < colors.size(); i++)
{
if (colors.get(i).substring(0, 1).equals("b"))
{
colors.remove(i);
}
}
colors
[red, brown]
colors.add("red");
colors.add("blue");
colors.add("brown");
colors.add("black");
true
colors
[red, brown, red, blue, brown, black]
for (int i = colors.size() - 1; i > -1; i--)
{
if (colors.get(i).substring(0, 1).equals("b"))
{
colors.remove(i);
}
}
colors
[red, red]
类
保存不同类型的信息,就需要类,类的属性可以是基础类型,也可以是对象。
String 标准类
String s1 = "hello";
String s2 = "hello"; // 字面量字符串
s1 == s2
true
s1.equals(s2)
true
String s3 = new String("hello"); // new 表示新的对象,新的内存
s2.equals(s3)
true
s3 == s1
false
== 比较的是字符串的内存地址,如果是用双引号创建的字符串,且内容相同,存储在内存中相同的位置。
equals 比较的是字符串的内容,如果字符串的内容相同结果就是 true,所以一般比较字符串用 equals
方法比较
compareTo 方法
int compare(String s1, String s2)
{
int l1 = s1.length();
int l2 = s2.length();
int min = l1;
if (l2 < l1)
{
min = l2;
}
for (int i = 0; i < min; i++)
{
if (s1.charAt(i) != s2.charAt(i))
{
return s1.charAt(i) - s2.charAt(i);
}
}
return l1 - l2;
}
compare("abe", "abcd");
2
compare("abc", "abcde");
-2
compare("a", "b");
-1
compare("a", "def");
-3
compare("abcdef", "ac")
按值传递和按引用传递
int a = 2;
void add(int a, int b)
{
a = a + 10;
System.out.println(a + b);
}
System.out.println(a);
a 依然是 2 因为 primitive type 是按照值传递
int[] arr = {1, 2, 3};
for (int v: arr)
{
System.out.println(v + " ");
}
System.out.println("before call modifyArr method");
void modifyArr(int[] arr)
{
arr[1] = 100;
for (int v: arr)
{
System.out.println(v + " ");
}
System.out.println();
}
modifyArr(arr);
for (int v: arr)
{
System.out.println(v + " ");
}
System.out.println("after call modifyArr method");
1
2
3
before call modifyArr method
1
100
3
1
100
3
after call modifyArr method
arr 变了,是因为数组是对象,对象是按照引用传递
方法 modifyArr 有副作用,因为修改了参数。
不修改参数的方法
纯方法,没有副作用的方法
int[] arr = {1, 2, 3};
for (int v: arr)
{
System.out.println(v + " ");
}
System.out.println("before call modifyArr method");
void newArr(int[] arr)
{
// 创建参数的副本,避免修改原来的数组
int[] rst = new int[arr.length];
for (int i = 0; i < rst.length; i++)
{
rst[i] = arr[i];
}
rst[1] = 100;
for (int v: rst)
{
System.out.println(v + " ");
}
System.out.println();
}
newArr(arr);
for (int v: arr)
{
System.out.println(v + " ");
}
System.out.println("after call modifyArr method");
1
2
3
before call modifyArr method
1
100
3
1
2
3
after call modifyArr method
这种就是不改变参数的方法,没有副作用。
随机数
Math.random()
0.7382607016022088
Math.random() * 10
2.8036906566977846
(int)(Math.random() * 10)
3
类
-
-
-
-
-
-
-
method header or method signature
-
-
-
继承多态,动态绑定
class Animal
{
public void eat()
{
System.out.println("animal eat");
}
public String toString()
{
return "animal";
}
}
class Cat extends Animal
{
// override method
public void eat()
{
System.out.println("cat eat mouse");
}
}
class Dog extends Animal
{
// override method
public void eat()
{
System.out.println("dog");
}
public void bark()
{
System.out.println("bark");
}
}
Animal a1 = new Animal()
a1.eat()
animal eat
Cat c1 = new Cat()
c1.eat()
cat eat mouse
Dog d = new Dog()
d.eat()
dog
调用猫和狗的 eat 行为不同,因为子类重写父类的行为
a1
animal
c1
animal
d
animal
都是 animal 因为子类调用了父类的 toString 方法,子类没有重写 toString
Animal a2 = new Cat()
a2.eat()
cat eat mouse
所有的猫都是动物,所以可以把 Cat 类型的变量赋值给 Animal 类型的变量。
因为 Cat 是 Animal 的子类
Animal a3 = new Dog();
a3.bark()
| a3.bark()
cannot find symbol
symbol: method bark()
报错是因为 Animal 中没有定义 bark 方法。能执行什么方法取决于是甚么类型,而不是实际是什么对象。
实际执行那种方法,取决于实际上是甚么对象。
递归
int sum(int n)
{
if (n == 0)
{
return 0;
}
return n + sum(n - 1);
}
boolean what(String str)
{
if (str.length() == 1)
{
return true;
}
else if(str.substring(0, 1).compareTo(str.substring(1, 2)) > 0)
{
return what(str.substring(1));
}
else
{
return false;
}
}
what("abcde")
false
what("bacde")
false
递归算法 what 是判断字符串是否是降序。
int whatDo(int[] arr, int value, int len)
{
int k = 0;
if (arr[len-1] == value)
{
k += 1;
}
if(len == 1)
{
return 0;
}
return k + whatDo(arr, value, len - 1);
}
int[] arr = {1, 2, 3, 4, 3, 2, 1, 0};
whatDo(arr, 2, arr.length);
2
上述递归看 arr 中有多少个 value。
writing class
一般来说,名词当作属性,动词就是动作。
犀牛国际坚持小班化、个性化的教育模式,能让授课老师在最大程度关注到每一位学生的学习动态与知识掌握程度,用精英老师来培养精英学生,让学生学习优秀的方法,为学生埋下优秀的基因。
犀牛国际AP培训课程开设了精品小班、一对一等多种班型,家长和同学们可任意选择,线下+线上同步授课,在上海、北京、南京、苏州、无锡、杭州、广州、深圳、青岛、合肥、武汉、济南、成都等地均设有线下校区,注:各校区班型不同,具体扫描底部二维码咨询!
|