1.数组的拓展
1.1冒泡排序
时间复杂度:o(n$^2$)
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
| import java.util.Arrays;
public class Demo{ public static void main(String[] args){ int[] a = {1,34,5673,3,456,124,4562,23}; int[] sort = sorted(a); System.out.println(Arrays.toString(sort)); } public static int[] sorted(int[] a){ for (int i = 0;i < a.length-1;i++){ for (int j = 0;j < a.length-1-i;j++){ if (a[j+1] < a[j]){ int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } return a; } }
|
1.2稀疏数组

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
| public class Demo{ public static void main(String[] args){ int[][] array1 = new int[11][11]; array1[1][2] = 1; array1[2][3] = 2; System.out.println("输出原始数组:"); for (int[] x:array1){ for (int y:x){ System.out.print(y + "\t"); } System.out.println(); }
int sum = 0; for (int i = 0;i < 11;i ++){ for (int j = 0;j < 11;j ++){ if (array1[i][j] != 0){ sum ++; } } } System.out.println("有效数字的个数是:"+sum); int[][] array2 = new int[sum+1][3]; array2[0][0] = 11; array2[0][1] = 11; array2[0][2] = sum; int count = 1; for (int i = 0;i < array1.length;i ++){ for (int j = 0;j < array1[i].length;j ++){ if (array1[i][j] != 0){ array2[count][0] = i; array2[count][1] = j; array2[count][2] = array1[i][j]; count ++; } } } for (int[] x:array2){ System.out.println(x[0]+"\t"+x[1]+"\t"+x[2]); } int[][] array3 = new int[array2[0][0]][array2[0][1]]; for (int i = 1;i < array2.length;i++){ array3[array2[i][0]][array2[i][1]] = array2[i][2]; } for (int[] x:array3){ for (int y:x){ System.out.print(y+" "); } System.out.println(); } } }
|
2.面向对象编程(OOP)
Java的核心思想就是OOP。


3.静态方法与非静态方法
静态方法(类方法)的特点:只能使用参数和静态变量(包括自己类的和别的类的允许访问的),没有this自引用,但是可以通过new的对象或传入的参数对象来引用。
Demo1:new一个对象来调用非静态方法(随对象创建进行加载)。

Demo2:用类名来调用静态方法(随类进行加载)。

1 2 3 4 5 6 7 8 9 10
| public class Demo3{ public static void main(String[] args){} public void a(){ b(); } public void b(){ } }
|
1 2 3 4 5 6 7 8 9 10 11
| public class Demo4{ public static void main(String[] args){} public static void a(){ b(); } public void b(){ } }
|
1 2 3 4 5 6 7 8 9 10
| public class Demo5{ public static void main(String[] args){} public void a(){ b(); } public static void b(){ } }
|
总结:非静态方法可以调用非静态方法以及静态方法;静态方法不能调用非静态方法。
4.java中的方法都是值传递
1 2 3 4 5 6 7 8 9 10 11 12
| public class Demo1{ public static void main(String[] args){ int a = 1; Demo.change(a); System.out.println(a); } public static void change(int a){ a = 10; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class Demo2{ public static void main(String[] args){ Person person = new Person(); System.out.println(person.name); Demo.change(person); System.out.println(person.name); } public static void change(Person person){ person.name = "java"; } }
class Person{ String name; }
|
5.类与对象的创建

养成习惯,专门在一个主程序中写main方法,用于测试。在别的类中不要加上main方法。


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
| public class Student { String name; int age; public void study(){ System.out.println(this.name+"在学习"); } }
public class Demo{ public static void main(String[] args){ Student xiaoming = new Student(); Student xiaohong = new Student(); xiaoming.name = "小明"; xiaoming.age = 18; System.out.println(xiaoming.name); System.out.println(xiaoming.age); xiaoming.study(); } }
|
6.类的构造器(构造方法)
类中的构造方法是在进行创建对象时必须调用的。它只能在创建对象时被间接调用。
它有以下两个特点:
- 必须和类的名字相同
- 必须没有返回类型,也不能写void
如果类中什么都不写,它会存在默认构造方法。
1.使用new关键字本质上是在调用构造器
2.用来初始化值
1 2 3 4 5 6 7
| public class Person { String name; public Person(){ this.name = "wang"; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Person { String name; int age; public Person(){ } public Person(String name,int age){ this.name = name; this.age = age; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class Person { String name; int age; public Person(String name){ this.name = name; } public Person(String name,int age){ this(name); this.age = age; } } public class Application { public static void main(String[] args) { Person person = new Person("xiaoming",18); System.out.println(person.name); } }
|
7.创建对象内存分析

8.小结类与对象
对象的引用
引用类型:除8种基本类型之外的都是引用类型。
对象是通过引用来操作的(如上图):栈 —-> 堆(地址)。
属性
也即字段Field,成员变量
会进行默认初始化(缺省值):
- 数字:0 0.0
- char:
\u0000
- boolean:false
- 引用类型:null
类
1.属性:静态的属性
2.方法:动态的行为
9.OOP三大特性之封装

1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口get/set
4.系统可维护性增加
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
| public class Student { private String name; private int id; private char gender; private int age; public String getName(){ return this.name; } public int getAge(){ return this.age; } public void setName(String name){ this.name = name; } public void setAge(int age){ if (age >= 130 || age <= 0){ this.age = 3; }else{ this.age = age; } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class Demo{ public static void main(String[] args){ Student s1 = new Student(); s1.setName("student1"); System.out.println(s1.getName()); s1.setAge(999); System.out.println(s1.getAge()); s1.setAge(75); System.out.println(s1.getAge()); } }
|
10.OOP三大特性之继承

- 在Java中,所有的类都默认直接或者间接继承Object类
- Java中类只有单继承,没有多继承!一个儿子只能有一个爸爸,而一个爸爸可以有多个儿子
- 私有的东西无法被继承
- 子类拥有父类对象所有的属性和方法(包括私有属性和私有方法),但是父类中的私有属性和方法子类是无法访问,只是拥有
- 被final修饰的class不能被继承
- 子类可以通过this访问公共继承自父类的属性,相当于子类对象里藏着一个父类对象
- 就好像子类的引用可以一物二用,既可以当作父类的引用使用,又可以当作子类的引用
使用
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
| public class Person { private int money = 10_000; public void say(){ System.out.println("hello"); } public int getMoney(){ return this.money; } public void setMoney(int money){ this.money = money; } }
public class Student extends Person { }
public class Teacher extends Person{ }
public class Demo{ public static void main(String[] args){ Student student = new Student(); student.say(); System.out.println(student.getMoney()); } }
|
super演示(重要)
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
| public class Person { protected String name = "kuangshen"; public void print(){ System.out.println("Person"); } }
public class Student extends Person { private String name = "qingjiang"; public void test(String name){ System.out.println(name); System.out.println(this.name); System.out.println(super.name); } public void print(){ System.out.println("Student"); } public void test1(){ print(); this.print(); super.print(); } }
public class Demo{ public static void main(String[] args){ Student student = new Student(); student.test("请将"); student.test1(); } }
|
子类与父类的构造器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class Person { protected String name = "kuangshen"; public Person(){ System.out.println("Person无参执行了"); } }
public class Teacher extends Person{ public Teacher(){ super(); System.out.println("Teacher无参执行了"); } } public class Demo{ public static void main(String[] args){ Teacher teacher = new Teacher(); } }
|
super的注意点
super与this的不同点
- 代表的对象不同
- this没继承也能使用,super只能在继承时使用
- this();调用本类的构造,super();调用父类的构造
- this可以作为返回值,但super不可以