一、命名规范

包名:所有字母小写
类名,接口名:大驼峰 所有单词首字母大写
变量名,方法名:小驼峰 第一个单词首字母小写其余大写
常量名:所有字母大写

二、变量类型及其运算规律

1.基本数据类型

整数型 byte(1字节=8bit) short(2字节) int(四字节) long(8字节,声明时带l或L结尾)
浮点型 float double(声明时带f或F结尾)
字符型 char(只能一个字符=2字节 且通常用’ ‘单引号引)
布尔型 boolean

2.引用数据类型

类 class
接口 interface
数组 []

3.自动类型提升

容量小的数据类型和容量大的数据类型做运算时,结果类型自动提升为容量大的数据类型
byte、char、short —> int —> long —> float —> double
特别的:byte、char、short 三者做运算时结果自动转为int

4.强制类型转换

可能会造成精度损失
double a = 12.3;
int i = int(a); // 截断操作 i = 12

5.String类型变量

String不是基本数据类型属于引用数据类型
声明String类型变量时用” “(双引号引)
String可以与其他基本数据类型做+号运算(链接运算)运算结果仍是String

三、不同进制的表示及转换方式

1.进制表示

二进制(binary)以0b或0B开头
>十进制(decimal)啊这,用不着开头,直接写
>八进制(octal)以数字0开头
>十六进制(hex)0-9 以及Aa Bb Cc Dd Ee Ff 以0x或0X开头 eg.0x21AE+1=0x21AF

2.进制转换

二进制转十进制 啊这,阶乘,略
十进制转二进制 除2取余的逆(逐步剥离出来里面到底有几个2)
二进制转八进制和十六进制 八进制每三个一位 十六进制四个一位
综上四个进制的转换流程为:
↗—>八进制
十进制 —> 二进制 —> (二进制神!)
↘—>十六进制

庆幸的是我们的java库中也提供了进制转换函数(她真的,我枯死)
二进制 toBinary(int i)
八进制 toOctal(int i)
十六进制 toHex(int i)

3.原码、反码、补码三剑客

至于原码、反码、补码三剑客,引用我之前写的小文档解释,这里不加赘述。(无所谓
image.png

每日小结

1.标识符的命名规范有哪些?

包名:所有字母小写
类名,接口名:大驼峰 所有单词首字母大写
变量名,方法名:小驼峰 第一个单词首字母小写其余大写
常量名:所有字母大写

2.Java的8种基本数据类型和占用内存大小。

byte 1b short 2b int 4b long 8b
char 2b
float 4b double 8b
boolean 不考虑

3.基本数据类型自动类型提升的运算规则。

容量小的数据类型转化为容量大的数据类型
byte、char、short —> int —> long —> float —> double
特别的:byte、char、short 三者做运算时结果自动转为int

四、运算符的使用

1.算术运算符的注意点

byte、short、char变量自加及自减运算时:
仍会保持原类型不会自动类型提升

2.加练:取百、十、个位的算法

  1. public class change(){
  2. public static void main(String[] arg){
  3. int num = 187;
  4. int bai = num / 100; // 整除100取百位
  5. int shi = num % 100 / 10; // 取余100获取十、个位
  6. int ge = num % 10; // 取余10获取个位
  7. // 直接整除可得前几位,直接取余可获得后几位
  8. }
  9. }

3.比较运算符

a > b // 结果为布尔型变量

4.逻辑运算符的注意点

&逻辑且 |逻辑或 !非 ^异或
&&短路与 ||短路或
逻辑与短路的区别:
>相同点1:运算结果相同
>相同点2:左边符合条件时,都会继续判断右边
>不同点:左边就不符合条件时,&仍会判断右边,&&直接跳过
平常开发时经常用短路符

5.位运算符*

int i = 21;
System.out.println(i>>2);
21二进制为10101,左移俩位补0,为10010100 相当于*22
同理>>右移空位补1
>>>右移空位补0
~取反

6.加练:互换两个数

  1. public class change(){
  2. public static void main(String[] arg){
  3. // 定义两个数待交换
  4. int num1 = 10;
  5. int num2 = 20;
  6. // 定义临时变量,推荐
  7. int temp = num1;
  8. num1 = num2;
  9. num2 = temp;
  10. // 不用定义临时变量,但有可能相加溢出,且只能用于数值类型
  11. num1 = num1 + num2; // 求和
  12. num2 = num1 - num2; // 剥离出num1赋给num2
  13. num1 = num1 - num2; // 剥离出num2赋给num1
  14. }

7.三元(目也行)运算符

  1. public class change(){
  2. public static void main(String[] arg){
  3. // 获取两个整数的较大值
  4. int m = 12;
  5. int n = 10;
  6. int max = (m > n)? m : n;
  7. System.out.println(max);
  8. // 改写if-else
  9. if(m > n){
  10. System.out.println(m);
  11. }else{
  12. System.out.println(n);
  13. }
  14. // 以后能用三目运算符时就用
  15. }
  16. }

8.运算符的优先级

通常优先级由高到底的顺序依次是:
1 括号 ()
2 正负号 + -
3 一元运算符 ++,—, !
4 乘除 */ %
5 加减 + -
6 移位运算 >> <<
7 比较大小 < > >=
8 比较是否相等 == ,! =
9 按位与运算 &
10 按位异或运算 ^
11 按位异或运算 |
12 逻辑与运算 &&
13 逻辑或运算 ||
14 三元运算符 ?:
15 赋值运算符 =

每日小结

1.&和&&的区别

&&前面符合可以直接跳过后面条件

2.取三个数里的较大值

  1. public class max(){
  2. public static void main(String[] arg){
  3. // 获取三个整数的较大值
  4. int x = 12;
  5. int y = 10;
  6. int z = 13;
  7. int tempmax = (x > y)? x : y;
  8. int max = (tempmax > z)? tempmax : z;
  9. }
  10. }

3.交换两个变量的实现

  1. public class change(){
  2. public static void main(String[] arg){
  3. String hometown = "濮阳";
  4. String motherland = "祖国";
  5. //推荐交换方式
  6. String temp = hometown;
  7. hometown = motherland;
  8. motherland = temp;
  9. }
  10. }

五、流程控制

1.if-else结构使用

  1. public class ifset(){
  2. public static void main(String[] arg){
  3. // 第一种if
  4. if(条件表达式){
  5. 执行表达式
  6. }
  7. // 第二种if 二选一
  8. if(条件表达式){
  9. 执行表达式1
  10. }else{
  11. 执行表达式2
  12. }
  13. // 第三种if 多选一
  14. if(条件表达式){
  15. 执行表达式1
  16. }else if{
  17. 执行表达式2
  18. }else if{
  19. 执行表达式n
  20. }
  21. }
  22. }

2.使用Scanner从键盘获取数据

  1. // 导入Scanner包
  2. import java.util.Scanner;
  3. public class ScannerTest{
  4. public static void main(String[] args){
  5. // 创建Scanner的实例化对象并赐名scan
  6. Scanner scan = new Scanner(System.in);
  7. int score = scan.nextInt();
  8. System.out.println(score);
  9. /*
  10. *scan.next() // 获取字符串
  11. *scan.nextInt() // 获取整数
  12. *scan.nextDouble() // 获取双浮点数
  13. *scan.nextBoolean() // 获取布尔值
  14. */
  15. }
  16. }

3.swich-case结构使用

  1. /*
  2. *根据switch表达式中的值,依次匹配各个case中的常量。一旦匹配成功,则进入相应case结构中,调用其执行语句。当调用完执行语句以后,则仍然继续向下执行
  3. *其他case结构中的执行语句,直到遇到break关键字或此switch-case结构末尾结束为止
  4. *break,可以使用在switch-case结构中,表示一旦执行到此关键字,就跳出switch-case结构
  5. *break,记得加,大多数可能会出事儿(不排除少数情况...
  6. *switch后的表达式只能是byte short int String 枚举类型
  7. *case 之后只能声明常量。不能声明范围。
  8. */
  9. public class SwitchCaseTest(){
  10. public static void main(String[] arg){
  11. int num = 3;
  12. switch(num){
  13. case 0:
  14. System.out.printlln("zero");
  15. break;
  16. case 1:
  17. System.out.printlln("one");
  18. break;
  19. case 2:
  20. System.out.printlln("two");
  21. break;
  22. case 3:
  23. System.out.printlln("three");
  24. break;
  25. default:
  26. System.out.printlln("other");
  27. break;
  28. }
  29. }
  30. }

4.for循环结构使用

  1. public class ForTest{
  2. public static void main(String[] args){
  3. // 输出5次Hello,world!
  4. for(int i = 1;i <= 5;i++){
  5. System.out.println("Hello,world!");
  6. }
  7. // 练习 输出结果acbacbacbac
  8. int num = 1;
  9. for(Sytem.out.println("a");num <= 3;Sytem.out.println("b"),num++){
  10. Sytem.out.println("c");
  11. }
  12. }
  13. }

5.加练:取最大公约数和最大公倍数

  1. public class Test{
  2. public static void main(String[] args){
  3. int m = 100;
  4. int n = 999;
  5. // 公约数思路:取两者中较小值,自其开始循环递减,并做判断取余为0操作
  6. int min = (m < n)? m : n;
  7. // 下限为1
  8. for(int i = min;i >= 1;i--){
  9. if(m % i == 0 && n % i == 0){
  10. System.out.println(i);
  11. break;
  12. }
  13. }
  14. // 公倍数思路:取两者中较大值,自其开始循环递增,并做判断取余为0操作
  15. int max = (m > n)? m : n;
  16. // 上限为m * n
  17. for(int i = min;i <= m * n;i--){
  18. if(i % m == 0 && i % n == 0){
  19. System.out.println(i);
  20. break;
  21. }
  22. }
  23. }
  24. }

6.while循环结构使用

  1. // 输出5次Hello,world!
  2. public class Test{
  3. public static void main(String[] args){
  4. int i = 1;
  5. while(i <= 5){
  6. System.out.println("Hello,world!");
  7. i++; // 千万不要忘记迭代条件造成死循环
  8. }
  9. }
  10. }

6.do-while循环结构使用

  1. // 输出5次Hello,world!
  2. public class Test{
  3. public static void main(String[] args){
  4. int i = 1;
  5. // 不论如何至少执行一遍 可类比为先去买,买完在判断够不够
  6. do{
  7. System.out.println("Hello,world!");
  8. i++; // 千万不要忘记迭代条件造成死循环
  9. }while(i <= 5)
  10. }
  11. }

7.while(true)结构使用

  1. import java.util.Scanner;
  2. // 题目:从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序
  3. public class Test{
  4. public static void main(String[] args){
  5. Scanner scan = new Scanner(System.in);
  6. int positive=0;
  7. int negative=0; // 记录正负数个数
  8. while(true){
  9. int num = scan.nextInt();
  10. if(num < 0){
  11. negative++;
  12. }else if(num > 0){
  13. positive++;
  14. }else{
  15. break;
  16. }
  17. }
  18. }
  19. }

8.★★★嵌套循环(必看!)★★★

输出特定图形

  1. public class Test{
  2. public static void main(String[] args){
  3. /*输出:i行 j列
  4. ***** 1 5
  5. ***** 2 5
  6. ***** 3 5
  7. ***** 4 5
  8. ***** 5 5
  9. */
  10. // 外层循环控制行数,内存循环控制列数
  11. for(int i = 1;i <= 5;i++){
  12. for(int j = 1;j <= 5;j++){
  13. System.out.print("*");
  14. }
  15. System.out.print("\n");
  16. }
  17. /*输出:i行 j列
  18. * 1 1
  19. ** 2 2
  20. *** 3 3
  21. **** 4 4
  22. ***** 5 5
  23. */
  24. // 外层循环控制行数,内存循环控制列数
  25. for(int i = 1;i <= 5;i++){
  26. for(int j = 1;j <= i;j++){
  27. System.out.print("*");
  28. }
  29. System.out.print("\n");
  30. }
  31. /*输出:i行 j列
  32. ***** 1 5
  33. **** 2 4
  34. *** 3 3
  35. ** 4 2
  36. * 5 1
  37. */
  38. // 外层循环控制行数,内存循环控制列数
  39. for(int i = 1;i <= 5;i++){
  40. for(int j = 1;j <= 6 - i;j++){
  41. System.out.print("*");
  42. }
  43. System.out.print("\n");
  44. }
  45. }
  46. }

好了,你已经出师了,自行尝试写一下NineNineTable吧!

  1. public class NineNineTable{
  2. public static void main(String[] args){
  3. /* i行 j列
  4. 1*1=1 1 1
  5. 1*2=2 2*2=4 2 2
  6. ... . .
  7. 1*9=9 2*9=18... 9 9
  8. */
  9. // 输出九九乘法表
  10. for(int i = 1;i <= 9;i++){
  11. for(int j = 1;j <= i;j++){
  12. System.out.print(j + "*" + i + "=" + i*j + " ");
  13. }
  14. System.out.print("\n");
  15. }
  16. }
  17. }

输出质数的方式:

  1. public class Test{
  2. public static void main(String[] args){
  3. // 输出100以内的质数 最小的质数为2
  4. Boolean is_flag = true; // 定义一个布尔判断是否为质数,不能丢到循环里,有重复定义消耗内存
  5. for(int i = 2;i <= 100;i++){
  6. // 排除1和他本身
  7. for(int j = 2;j < i;j++){
  8. if(i % j == 0){
  9. is_flag = false;
  10. break; // 有一个就可以停了
  11. }
  12. }
  13. if(is_flag == true){
  14. System.out.println(i);
  15. }
  16. is_flag = true; // 重置状态
  17. }
  18. }
  19. }

具体优化方式后续补充……

阶段项目一(待填坑)

模拟实现一个基于文本界面的《家庭记账软件》掌握初步的编程技巧和调试技巧

  1. public class FamilyAccount(){
  2. public static void main(String[] args){
  3. //学到面向对象再填坑
  4. }
  5. }

六、数组

1.概述

1)数组是有序排列的
2)数组属于引用数据类型的变量。数组的元素,既可以是基本数据类型,也可以是引用数据类型
3)创建数组对象会在内存中开辟一整块连续的空间
4)数组的长度一旦确定,就不能修改

2.声明、赋值、引用与遍历

```java public class Test { public static void main(String[] args) { // 声明 String[] names = new String[5]; // 赋值 names[0] = “名字1”; names[1] = “名字2”; names[2] = “名字3”; names[3] = “名字4”; names[4] = “名字5”; // 属性length System.out.println(names.length); // 遍历数组 for(int i = 0;i< names.length;i++){ System.out.println(names[i]); } } }

  1. <a name="kzHu6"></a>
  2. ### 3.借用数组解释内存的简单结构(了解)
  3. `int[] arr = int[]{1,2,3};`<br />`String[] arr1 = new String[4];` <br />![](https://cdn.nlark.com/yuque/0/2022/jpeg/25472905/1646548567126-c2ab2b28-819e-477d-8064-d2b0b1497972.jpeg)
  4. <a name="hfMJC"></a>
  5. ### 4.关于租房数组小练习
  6. 升景坊单间短期出租4个月,550元/月(水电煤公摊,网费35元/月),空调、卫生间、厨房齐全。屋内均是IT行业人士,喜欢安静。所以要求来租者最好是同行或者刚毕业的年轻人,爱干净、安静。
  7. ```java
  8. public class Test {
  9. public static void main(String[] args) {
  10. // 结果为18013820100 试着 自行分析
  11. int[] arr = new int[]{8,2,1,0,3};
  12. int[] index = new int[]{2,0,3,2,4,0,1,3,2,3,3};
  13. String tel = "";
  14. for(int i = 0;i < index.length;i++){
  15. tel += arr[index[i]];
  16. }
  17. System.out.println("联系方式: " + tel);
  18. }
  19. }

5.二维数组声明、赋值、引用和遍历

  1. public class Test {
  2. public static void main(String[] args) {
  3. // 二维数组的声明及赋值
  4. int[][] arr = new int[][]{{1,2,3},{4,5,6}};
  5. String[][] arr1 = new String[3][2];
  6. // 二维数组调用(借用矩阵了解)
  7. System.out.println(arr[0][0]); // 第1行第1列 为1
  8. System.out.println(arr[1][2]); // 第2行第3列 为6
  9. // 二维数组遍历(借用嵌套循环理解)
  10. for(int i = 0;i < arr.length;i++){
  11. for(int j = 0;j < arr[i].length;j++){
  12. System.out.println(arr[i][j]);
  13. }
  14. }
  15. }
  16. }

6.杨辉三角小练习

  1. public class Test {
  2. public static void main(String[] args) {
  3. /*
  4. 使用二维数组打印十行的杨辉三角
  5. 提示:
  6. 1.行有1个元素,第n行有n个元素
  7. 2.每1行的第1个元素和最后一个元素都是1
  8. 3.从第三行开始,对于非第一个元素和最后一个元素的元素。即:
  9. yanghui[i][i] =yanghui[i-1][j-1] + yanghui[i-1][j];
  10. */
  11. // 声明数组的第一维 即10行
  12. int[][] yanghui = new int[10][];
  13. // 开始赋值
  14. for(int i = 0;i < yanghui.length;i++){
  15. // 确认第二维的数组储存数据个数 对应提示1
  16. yanghui[i] = new int[i+1];
  17. // 赋予首位、末位元素初值 对应提示2
  18. yanghui[i][0] = yanghui[i][i] = 1;
  19. // 为中间元素赋值 对应提示3
  20. for(int j = 1;j < yanghui[i].length - 1;j++){
  21. yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j];
  22. }
  23. }
  24. // 遍历
  25. for(int i = 0;i < yanghui.length;i++){
  26. for(int j = 0;j < yanghui[i].length;j++){
  27. System.out.print(yanghui[i][j] + "\t");
  28. }
  29. System.out.println();
  30. }
  31. }
  32. }

7.类似C语言的数组名称含义(即第一个元素的地址指针)

  1. public class Test {
  2. public static void main(String[] args) {
  3. int[] array1,array2;
  4. array1 = new int[]{1,2,3,4,5,6};
  5. // 输出array1内容 为123456
  6. for(int i = 0;i < array1.length;i++){
  7. System.out.print(array1[i]);
  8. }
  9. System.out.println();
  10. // 将数组1的名字赋给数组2
  11. array2 = array1;
  12. // 在数组2更改值
  13. array2[0] = 3;
  14. // 再次输出array1内容 发现变为323456
  15. for(int i = 0;i < array1.length;i++){
  16. System.out.print(array1[i]);
  17. }
  18. }
  19. }

8.数组复制

  1. public class Test {
  2. public static void main(String[] args) {
  3. int[] array1 = new int[]{1,2,3,4,5};
  4. // 创建备份数组,且长度相等
  5. int[] beifen = new int[array1.length];
  6. // 遍历原数组并赋给备份数组
  7. for(int i = 0;i < array1.length;i++){
  8. beifen[i] = array1[i];
  9. }
  10. // 遍历备份数组
  11. for(int i = 0;i < beifen.length;i++){
  12. System.out.print(beifen[i]);
  13. }
  14. }
  15. }

9.数组反转

方法一:一个变量i两边用

  1. public class Test {
  2. public static void main(String[] args) {
  3. int[] array1 = new int[]{1,2,3,4,5};
  4. // 方法一 一个变量i两边用
  5. for(int i = 0;i < array1.length / 2;i++){
  6. int temp = array1[i];
  7. array1[i] = array1[array1.length -1 - i];
  8. array1[array1.length -1 - i] = temp;
  9. }
  10. // 遍历输出数组
  11. for(int i = 0;i < array1.length;i++){
  12. System.out.print(array1[i]);
  13. }
  14. }
  15. }

方法二:两个变量i,j两头一起进军

  1. public class Test {
  2. public static void main(String[] args) {
  3. int[] array1 = new int[]{1,2,3,4,5};
  4. // 方法一 两个变量i,j两头一起进军,判断条件很巧妙建议细品
  5. for(int i = 0,j = array1.length - 1;i < j;i++, j--){
  6. int temp = array1[i];
  7. array1[i] = array1[j];
  8. array1[j] = temp;
  9. }
  10. // 遍历输出数组
  11. for(int i = 0;i < array1.length;i++){
  12. System.out.print(array1[i]);
  13. }
  14. }
  15. }

10.数组算法-线性查找

  1. public class Test {
  2. public static void main(String[] args) {
  3. // 线性查找
  4. String[] names = new String[]{"Tom", "Andy", "Angel", "Zack"};
  5. String destination = "yao_miao";
  6. boolean is_flag = false; //不用旗子的话 也可以判断i的次数
  7. // 遍历数组判断
  8. for(int i = 0;i < names.length;i++){
  9. if(destination.equals(names[i])){
  10. System.out.println("找到了索引为" + i);
  11. is_flag = true;
  12. break;
  13. }
  14. }
  15. if(!is_flag){
  16. System.out.println("没找到捏");
  17. }
  18. }
  19. }

11.数组算法-二分法

  1. public class Test {
  2. public static void main(String[] args) {
  3. // 二分法查找 前提:数组为有序
  4. int[] array = new int[]{-94, -88, -50, -22, 2, 5, 9, 25, 50, 66};
  5. int destination = 25;
  6. // 设置数组索引的前后边界
  7. int min = 0;
  8. int max = array.length - 1;
  9. // 小旗子
  10. boolean is_flag = false;
  11. // 二分法循环体
  12. while(min < max){
  13. int middle = (min + max) / 2;
  14. if(destination == array[middle]){
  15. System.out.println("找到了索引为" + middle);
  16. is_flag = true;
  17. break;
  18. }else if(destination < array[middle]){
  19. max = middle - 1;
  20. }else if(destination > array[middle]){
  21. min = middle + 1;
  22. }
  23. }
  24. if(is_flag == false){
  25. System.out.println("没找到捏");
  26. }
  27. }
  28. }

12.数组算法-冒泡排序

  1. public class Test {
  2. public static void main(String[] args) {
  3. int[] array = new int[]{10,56,34,4,68,214,65,-325,545,4};
  4. int temp;
  5. // 开始冒泡 外层为length-1轮 内存为每个元素比较 lenth-1-i轮
  6. for(int i = 0; i < array.length - 1;i++){
  7. for(int j = 0;j < array.length - 1 - i;j++){
  8. if(array[j] < array[j+1]){
  9. temp = array[j];
  10. array[j] = array[j+1];
  11. array[j+1] = temp;
  12. }
  13. }
  14. }
  15. // 遍历输出数组
  16. for(int i = 0;i < array.length;i++){
  17. System.out.print(array[i] + ",");
  18. }
  19. }
  20. }

每日小结

1.一维数组初始化的两种方式

int[] arr = new int[5]; // 动态初始化
String[] arr1 = new String[]{"Tom","Andy","Angel"}; // 静态初始化

2.二维数组初始化的两种方式

int[][] arr1 = new int[4][3]; // 动态初始化1
int[][] arr2 = new int[4][]; // 动态初始化2
String[] arr3 = new String[][]{{"蕾姆","漂亮"},{"亚里亚","suki"},{"嘉然","然然你带我走吧,然然,你带我走吧"}}; // 静态初始化

3.如何遍历以下二维数组

int[][] arr = new int[][]{{1,2,3},{4,5,6}};
见上……

4.不同类型的一维数组元素的默认初始化值

int 0
double 0.0
char 0
boolean false
引用类型 null

5.各种查找的算法和排序算法

见上……