Java basic 1

Array copy

1
2
3
4
5
6
7
8
9
int[] array1, array2;
array1 = new int[] {2,3,5,7,8,9,12,11,10};

//copy array1 to array2
array2 = new int[array1.length];
for(int i = 0; i < array2.length; i++)
{
array2[i] = array1[i];
}

我们通过new的方式,给array2在heap空间中开辟了新的数组空间,将array1数组的元素值一个个的copy进array2里

Swap array

1
2
3
4
5
6
for(int i = 0; i < arr.length / 2; i++)
{
String temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}

Search elements in an array

  1. linear search: 通过遍历的方式,一个一个的对比查找,具有普遍适用性
  2. binary search:
    每次比较中间值,< nums[mid] 找左边。数组必须有序

Sorting algorithms

  1. Selection sort O(n^2)
  2. Bubble sort O(n^2)
  3. Quick sort O(nlogn)
  4. Insertion sort O(n^2)
  5. Merge sort O(nlogn)
  6. Heap sort O(nlogn)

Time complexity, space compelxity are two of the most important keys

外部排序需要借助disk

Bubble sort

1
2
3
4
5
6
7
8
9
10
11
12
13
for(int i = 0; i < arr.length - 1; i++)
{
for(int j = 0; j < arr.length - 1 - i; j++)
{
if(arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

Arrays

  1. using java util package
  2. Arrays: 提供了 很多操作数组的方法

Procedure-oriented & Objected-oriented

Procedure-oriented: 强调功能行为,以函数为最小单位,考虑怎么做

  1. 把冰箱门打开
  2. 抬起大象,塞进冰箱
  3. 关冰箱门

Objected-oriented:
具备了功能的对象,以class/object为单位,考虑谁来做

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
person
{
new fridge()
{
fridge.open()
{

new elephant()
{
elephant.enter();
}
}

fridge
{
open();
}

elephant
{
enter();
}

OOP可以在现有的基础之上直接改class,但是POP要深入到程序里改。大项目里OOP优势明显。

OOP three features

  1. Encapsulation
  2. Inheritance
  3. Polymorphism

OOP concepts

Class and Object is the Key concept of OOP

Class: 抽象的,概念上的定义
Object: 实际存在的个体,so called instance
面向对象的程序设计的重点是object的设计,
设计object, 就是设计Object的成员

OOP program

  1. 创建class,设计class的成员

  2. 创建class的object

  3. Call object.filed OR object.methods 调用Object的结构

  4. 如果创建了一个class的多个object,每个object都有独立的一个class field(non- static) 如果我们修改一个对象的field a,不影响另外一个object的field. 除非我们让两个object指向heap里的同一个区域, 那么更改其中一个,另一个也会改变。

  5. Heap 存放 Object instantiation
    Stack 存放 Local variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class PersonTest
{
public static void main(String[] args)
{
//创建person类的对象 = 类的实例化
//每new一个object,heap里就多一个空间
Person p1 = new Person();

//call object: object.field
p1.name = "Tom";
p1.isMale = true;

//call methods: object.method
p1.eat();
p1.sleep();
p1.talk("English");

//**********************************
Person p2 = new Person();
System.out.println(p2.name); //null
System.out.println(p2.isMale); //false


//**********************************
//将P1变量保存的obejct address给p3,导致p1和p3指向了heap里的同一个block
Person p3 = p1;
System.out.println(p3.name); //Tom

p3.age = 10;
System.out.println(p1.age); //10




}
}


class Person
{
//属性 -> field
String name;
int age = 1; //可以赋值也可以不赋值
boolean isMale;//default is false

//方法 -> method
public void eat()
{
System.out.println("People eat");
}

public void sleep()
{
System.out.println("People sleep");
}

public void talk(String language)
{
System.out.println("People talk,
by speaking " + language);
}
}

Object memory analysis

Person p1 = new Person(); new memory in heap
p1.name = “Tom”;
The variable in methods are all local variable
p1.isMale = true;

Person p2 = new Person();
System.out.println(p2.name);
Person p3 = p1;
p3.age = 10;

stack p1 0x12b
–> p2 0x778
p3 0x12b

heap
name: null -> “Tom”
age: 1
isMale: false -> “true”
–>P2 name: null
age: 1
isMale: false

Call Field in Object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

// member variable VS local variable
1.相同点:
1.1 定义variable的格式,data type, variable name
1.2 先声明,后使用
1.3 都有scope

2.不同点:
2.1 在class中intialize的位置不同
field: 直接定义在class的一对{}里

local variable: 在method内,method arguments, constructor arguments, variable inside constructor 都是local variable出现的位置

2.2 Access modifiers
member variable 可以在声明时,指明权限
常用的access modifiers
private, public, protected, default

2.3 Default values
(byte, short, int, long): 0
(float, double) 0.0
(char) 0 (or '\u0000')
(boolean), false
reference data type: null

class User
{
// 2.1 field OR member variable OR instance variable

String name;
int age;
bool isMale;

// language is an arguments
//所以是local variable
public void talk(String language)
{
System.out.println("We use " + language);
}

public void eat()
{
//定义在method内的variable,是local variable
//不能加public 或者 private
//因为是local variable,方法是public,里面的local variable就也是public了
String food = "taco";
System.out.println("We like " + food);
}
}

Access modifiers

member variable 可以在声明时,指明权限
常用的access modifiers
private, public, protected, default
*(Encapsulation)*

Local variable cannot use access modifier

Find index of min and max value in an array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Scanner scan = new Scanner(System.in);
import java.util.*;
public class SpringPractice01
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int[] array = {4,2,-7,6,8,1,-7};
int min_index = 0;
for(int j = 0; j < array.length; j++)
{
if(array[min_index] > array[j])
{
min_index = j;
}
}

for(int i = 0; i < array.length; i++)
{
if(array[min_index] == array[i])
{
min_index = i;
System.out.println(min_index);
}
}
System.out.println(array[min_index]);
}
}






Selection sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int[] arr = {4,2,1,8,3};
for(int i = 0; i < arr.length - 1; i++)
{
int min = arr[i];
int index = i;
for(int j = i + 1; j < arr.length; j++)
{
if(arr[j] < min)
{
min = arr[j];
index = j;
}
}
if(index != i)
{
int temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}

Two-D array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
1
2,2
3,3,3
4,4,4,4
*/
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int[][] arr;
arr = new int[num][];
for(int i = 0; i < arr.length; i++)
{
arr[i] = new int[i + 1];
}

for(int i = 0; i < arr.length; i++)
{
for(int j = 0; j < arr[i].length; j++)
{
arr[i][j] = i + 1;
}
}
for(int i = 0; i < arr.length; i++)
{
for(int j = 0; j < arr[i].length; j++)
{
System.out.print(arr[i][j]);
}
System.out.println();
}

Income and outcome management

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//Scanner scan = new Scanner(System.in);
import java.util.*;
public class SpringPractice01
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int choice;
double deposite = 10000;
String details = "Income\tbalance\tAmount\tInfo\n";


do {
System.out.println("\t\t1. Income and outcome");
System.out.println("\t\t2. Register income");
System.out.println("\t\t3. Register outcome");
System.out.println("\t\t4. Quit");
choice = scan.nextInt();;
switch (choice)
{
case 1:
System.out.println("You have $ " + deposite);
System.out.println(details);
break;
case 2:
System.out.println("Please enter your earned");
double money = scan.nextDouble();
System.out.println("Please where the money from");
String info = scan.next();
deposite += money;
details += "Earned\t" + money + "\t" + deposite
+ info + "\n";
break;
case 3:
System.out.println("Please Register outcome");
money = scan.nextDouble();
System.out.println("Please where the money goes");
info = scan.next();
deposite -= money;
details += "Spent\t" + money + "\t" + deposite
+ info + "\n";
break;
case 4:
System.out.println("Sure to quit ? Y/N");
char select = scan.next().charAt(0);
if(select == 'Y')
{
choice = -1;
}
else
{
break;
}
}
}while(choice != -1);

}
}