冒泡排序
public class Demo01_冒泡排序 {
public static void main(String[] args) {
// 发现 数据与 比较的轮数有关系, 轮数 = 数据的长度-1
// 正好把 数据的轮数当成外层循环
int[] arr = {12, 3, 5, 35, 1};
for (int i = 0; i < arr.length - 1; i++) {
// 确定每一轮比较的次数
for (int j = 0; j < arr.length - 1 - i; j++) {
// System.out.println("第" + (i + 1) + "轮,比较了第" + (j+1) + "次");
// 进行数据的比较
// 比较前面的数据和后面的数据
if (arr[j] > arr[j + 1]) {
// 如果前面的数据比后面的数据大,交换数据
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// 遍历数组
for (int index = 0; index < arr.length; index++) {
System.out.println(arr[index]);
}
}
}
实例化对象
public class Demo02_实例化对象 {
public static void main(String[] args) {
// 创建对象 又称 实例化对象
// 类名 对象名 = new 类名();
Car car1 = new Car();
// 对象创建出来以后,通过 对象名. 对成员(变量或方法) 进行调用
// 成员变量
// 给成员变量赋值
car1.color = "紫色";
car1.num = 4;
// 取成员变量的值
System.out.println(car1.color); // 紫色
System.out.println(car1.num); // 4
// 成员方法
car1.run();
System.out.println("============================");
Phone phone1 = new Phone();
// 创建了 对象 但是没有给成员变量先赋值
System.out.println(phone1.brand); //null
System.out.println(phone1.price); // 0
System.out.println(phone1.size); // 0.0
// 当创建对象之后,如果没有给成员变量赋值,那么jvm会自动给成员变量赋默认值
// 整数 0; 小数 0.0; 布尔 false; 字符 '\u0000'; 引用数据类型 null;
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Student stu = new Student();
}
}
案例
//案例
public class Car {
//案例1:定义一个汽车类,并生产出汽车,有颜色,轮胎个数, 有跑的功能。
// 属性 成员变量 全局变量
String color;
int num;
// 行为 成员方法
public void run() {
System.out.println("小汽车跑起来了");
}
}
public class Phone {
//案例2:定义手机类
//属性: 品牌、颜色、价格、尺寸
//方法: 打电话、发短信
// 属性 成员变量 全局变量
String brand; // 品牌
String color; // 颜色
int price; // 价钱
double size; // 尺寸
// 行为 成员方法
public void call(){
System.out.println("手机可以打电话");
}
// 发短信
public void sendMessage(){
System.out.println("手机可以发送信息");
}
}