Java学习笔记
1、回滚昨天习题
package com.mao.day03; public class BackDay { public static void main(String[] args) { int[] array = {10, 20, 30, 40, 50}; int[] reverse = new int[array.length]; printTriangle(); printNine(); printPrimeNum(); printArray(array); System.out.println("反转后的数组为:"); reverse = printAdverse(array); for (int i = 0; i < reverse.length; i++) { System.out.print(reverse[i] + "\t"); } } //打印三角形 public static void printTriangle() { for (int i = 1; i <= 5; i++) { //先打印前面的空格 for (int j = 5; j >= i; j--) { System.out.print(" "); } //再打印左边的星号 for (int j = 1; j <= i; j++) { System.out.print("*"); } //最后打印右边的星号,第一行不打印,所以j < i for (int j = 1; j < i; j++) { System.out.print("*"); } System.out.println(); } } //打印99乘法表 public static void printNine() { // int sum = 0; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(i + "*" + j + "=" + (i * j) + "\t"); if (j == i) System.out.println(); /* sum++; if(sum%5==0) System.out.println();*/ } // System.out.println(); } } //打印100到500的所有质数 public static void printPrimeNum() { int num = 0; outer: for (int i = 100; i <= 500; i++) { for (int j = 2; j <= i / 2; j++) { if (i % j == 0) continue outer; } if (num % 5 == 0) System.out.println(); num++; System.out.print(i + "\t"); } System.out.println(); } //打印数组 public static int[] printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + "\t"); } System.out.println(); return array; } //反转数组 public static int[] printAdverse(int[] array) { int[] result = new int[array.length]; for (int i = 0, j = array.length - 1; i < array.length; i++, j--) { result[j] = array[i]; } return result; } } /*反思: 1. 一定要有逻辑思维,思考再动笔 2. 99乘法表的内外层循环要知道是在干什么,逻辑不能乱 */
2、二维数组
//其中array[i].length一定要搞清楚是什么!他代表的是二维数组中第i个元素的一维数组,获取其长度是因为要依次遍历它!! public class DoubleArrays { public static void main(String[] args) { int[][] array = {{1, 2}, {2, 3}, {3, 4}, {4, 5}}; //遍历二维数组的长度,共包含多少个一维数组 for (int i = 0; i < array.length; i++) { //二维数组的一维展示,从二维数组中每个位置进入当前的一维数组中 for (int j = 0; j < array[i].length; j++) { System.out.println(array[i][j]); } } } }
3、稀疏数组
-
首先,先构思好框架,我们所需要做的是N行3列的一个表格
-
那么第一步,构建第一个元素array[0] [0]则是决定行 array[0] [1]决定列 array[0] [2]则是决定整个表格中的非零元总个数
-
而接下来的每一行所展示的是,某一行某一列的值具体是多少
-
-
程序思想:
-
先记录棋盘中,有几行几列且记录其具体的值
-
然后依次遍历每一行把array1中每一个非零元素拿出来存进array2
-
如何还原?
-
搞清楚第一行,属性行的表示:array[0] [i]
-
下面的每一具体行,array[i] [0] array[i] [1] array[i] [2]
-
-
4、三元运算符
public class DoubleArrays { public static void main(String[] args) { int c = 0; //此处,直接使用(类名.方法名)进行调用------与下文的方法调用苟同,若在同一个类下,其实不需要(类.方法名)调用也可以 c = DoubleArrays.compareNums(8, 5); System.out.println(c); } public static int compareNums(int a, int b) { return a > b ? a : b; } }
5、静态方法和非静态方法
-
在同一个包中,各个类若想要被主方法main所调用,那么此方法应该为static静态方法
-
若为非静态方法,则需要通过实例化对象进行调用:也即我们最常用的--------new对象
//若需要在代码中进行调用此类的方法,则可以student.say();直接调用 //实例化这个类 new //对象类型 对象名 = 对象值; //Student student = new Student();
6、值传递和引用传递
//值传递(Java都是值传递) public class DoubleArrays { public static void main(String[] args) { int a = 1; //未进入方法,直接输出a为1 System.out.println(a); //进入方法,但是通过debug可知,进入方法的时候a=1,在方法体中a=10的赋值后,a为10;但后续退出方法体,且没有return返回值,那么进入主函数main,a又会变回全局属性1,进而输出 changeNum(a); System.out.println(a); } public static void changeNum(int a) { a = 10; } } //引用传递(实际上就是在主函数的基础上,去构建一个新的类(进而构建一个新的对象))然后在主函数中进行调用改变类中的具体属性 public class DoubleArrays { public static void main(String[] args) { Person person = new Person(); //未定义具体属性值之前的输出结果 System.out.println(person.name); //调用change方法,修改完成后会进行返回,进而进行输出 DoubleArrays.changeName(person); System.out.println(person.name); } public static void changeName(Person person) { person.name = "毛锋程"; } } class Person{ String name; }
7、类与对象的创建
-
单独创建一个类,其中包含类的属性、方法,如Student类,这个Student类的属性是“所有人”共有的,所以在2中
-
this是指代这个类、this.name也就是这个Student中的name属性
public class Student { String name; int age; String sex; public void say() { System.out.println("我的名字是:" + this.name + "\t我的年龄是:" + this.age + "\t我的性别是:" + this.sex); } }
-
创建一个测试类,在main方法中new一个新的对象,对其他类的方法、属性进行调用,new出对象后,对象名可以有多个
//Student类对象,同时可以拥有多个对象,如studentM、studentC,他们可以同时去调用Student类中的属性和方法 //studentM和studentC其实就是Student类的两个具体实例 public class Main { public static void main(String[] args) { Student studentM = new Student(); Student studentC = new Student(); studentM.name = "毛锋程"; studentM.age = 24; studentM.sex = "男"; studentC.name = "陈德蔓"; studentC.age = 24; studentC.sex = "女"; studentM.say(); System.out.println(); studentC.say(); } }
8、构造器(无参构造、有参构造)ALT + INSERT
-
系统默认会有一个无参构造,特殊情况可以不写
-
定义有参的时候,括号内需要带有参数的类型及名称
-
在有参构造中
-
this.name = name; 第一个this.name是整个类的name
-
第二个name则是构造器中的name(传进来的参数值)
-
-
-
类中定义了有参构造时,那么无参构造必须预先定义好(可以单空,什么内容都不写),否则在主函数中,new对象时会报错
-
构造器:
-
必须与类名相同
-
没有返回值
-
-
作用:
-
new的本质就是在调用构造方法
-
需要初始化对象的值
-
//对象类型 对象名 = 对象值; //Student student = new Student(); //new出来的对象,括号内可以写参数,这时候可以同名对象名,但是参数可以不一致(包括数量、值等),这样就可以进行方法的重载 //用于测试的Main类 public class Main { public static void main(String[] args) { Person person = new Person("毛锋程", 24); System.out.println("我的名字是:" + person.name + "\t我的性别是:" + person.age); } } //单独定义的Person类 public class Person { //定义类的属性字段 String name; int age; //空参构造、在出现有参构造时,空参必须显示写出 public Person() { } //有参构造,系统会自动识别(根据后面的属性类型、属性个数) public Person(String name, int age) { //this是指当前类的属性 //而等号后面的是指传过来的参数值 this.name = name; this.age = age; } }
9、new对象的 “内存” 展示
-
私有对象、Private需要用get、set方法进行调用,而Public则只需要构造器的普通方法this即可
-
栈中存放的是 “ 引用变量名 ” 而堆中存放的则是“ 具体创建出来的对象 ”
-
先是从main函数开始,然后new一个新的对象,对象中包含什么属性和方法--->接着再将堆里具体的对象值从初始进行更新
-
对象是通过引用来操作的,从栈------>堆
-