目录
学习目标:
- Java分支语句之if…else
- 循环的嵌套
- 方法的定义
- 方法的形参和实参
- 方法的递归调用
学习内容:
Java 修饰符
Java语言提供了很多修饰符,主要分为以下两类:
-
访问修饰符
default (什么也不写): 在同一包内可见,不使用任何修饰符。使用对象:类、接口、变量、方法。
private : 在同一类内可见。使用对象:变量、方法。 注意:不能修饰类(外部类)
public :对所有类可见。使用对象:类、接口、变量、方法
protected : 对同一包内的类和所有子类可见。使用对象:变量、方法。 注意:不能修饰类(外部类)。
修饰符 | 当前类 | 同一包内 | 子孙类(同一包) | 子孙类(不同包) | 其他包 |
---|---|---|---|---|---|
public | Y | Y | Y | Y | Y |
protected | Y | Y | Y | Y/N(说明) | N |
default | Y | Y | Y | N | N |
private | Y | N | N | N | N |
-
非访问修饰符
static 修饰符,用来修饰类方法和类变量。
final 修饰符,用来修饰类、方法和变量,final 修饰的类不能够被继承,修饰的方法不能被继承类重新定义,修饰的变量为常量,是不可修改的。
abstract 修饰符,用来创建抽象类和抽象方法。
synchronized 和 volatile 修饰符,主要用于线程的编程。(未掌握)
static 修饰符
·静态变量:
static 关键字用来声明独立于对象的静态变量,无论一个类实例化多少对象,它的静态变量只有一份拷贝。 静态变量也被称为类变量。局部变量不能被声明为 static 变量。
·静态方法:
static 关键字用来声明独立于对象的静态方法。静态方法不能使用类的非静态变量。静态方法从参数列表得到数据,然后计算这些数据。
final 修饰符
·final 变量:
final 表示"最后的、最终的"含义,变量一旦赋值后,不能被重新赋值。被 final 修饰的实例变量必须显式指定初始值。final 修饰符通常和 static 修饰符一起使用来创建类常量。
·final 方法
1、类中的 final 方法可以被子类继承,但是不能被子类修改。
2、声明 final 方法的主要目的是防止该方法的内容被修改。
·final 类
final 类不能被继承,没有类能够继承 final 类的任何特性。
abstract 修饰符
·抽象类:
1、抽象类不能用来实例化对象,声明抽象类的唯一目的是为了将来对该类进行扩充。
2、一个类不能同时被 abstract 和 final 修饰。如果一个类包含抽象方法,那么该类一定要声明为抽象类,否则将出现编译错误。
3、抽象类可以包含抽象方法和非抽象方法。
·抽象方法
1、抽象方法是一种没有任何实现的方法,该方法的的具体实现由子类提供。
2、抽象方法不能被声明成 final 和 static。
3、任何继承抽象类的子类必须实现父类的所有抽象方法,除非该子类也是抽象类。
4、如果一个类包含若干个抽象方法,那么该类必须声明为抽象类。抽象类可以不包含抽象方法。
5、抽象方法的声明以分号结尾。
Java分支语句之if...else
循环的嵌套(✔)
-
while 循环
public class DataDemo {
public static void main(String args[]) {
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
-
do…while 循环
public class DataDemo {
public static void main(String args[]) {
int x = 10;
do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
}
}
-
for 循环
public class DataDemo {
public static void main(String args[]) {
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
//这种写法增加条件逻辑判断
for (int i = 0; i <20;) {
//如果满足某种条件 i自增,
if (true){
i++;
}
}
}
}
-
forEach循环
public class DataDemo {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String [] names ={"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}
-
switch case
public class DataDemo {
public static void main(String args[]) {
//char grade = args[0].charAt(0);
char grade = 'C';
switch(grade)
{
case 'A' :
System.out.println("优秀");
break;
case 'B' :
System.out.println("良好");
break;
case 'C' :
System.out.println("一般");
break;
case 'D' :
System.out.println("及格");
break;
case 'F' :
System.out.println("你需要再努力努力");
break;
default :
System.out.println("未知等级");
}
System.out.println("你的等级是 " + grade);
}
}
延伸 :
保留两位小数:
1、Math.round((a*100)/100f) a不足两位小数时,只会保留一位小数
2、System.out.printf("%.2f\n", temp); 用c语言的结构来要求输出保留两位小数
3、DecimalFormat df2 = new DecimalFormat("0.00");
String p=df2.format(price); 转化为字符串形式来规定保留两位小数
Iterator类:
迭代器模式:就是提供一种方法对一个容器对象中的各个元素进行访问,而又不暴露该对象容器的内部细节。
hasNext() 判断时候存在下一个对象元素
next() 获取下一个元素
remove() 移除一个元素
Iterator和forEach循环比较:
采用ArrayList对随机访问比较快,而for循环中的get()方法,采用的即是随机访问的方法,因此在ArrayList里,for循环较快
采用LinkedList则是顺序访问比较快,iterator中的next()方法,采用的即是顺序访问的方法,因此在LinkedList里,使用iterator较快
从数据结构角度分析,for循环适合访问顺序结构,可以根据下标快速获取指定元素.而Iterator 适合访问链式结构,因为迭代器是通过next()和Pre()来定位的.可以访问没有顺序的集合.
而使用 Iterator 的好处在于可以使用相同方式去遍历集合中元素,而不用考虑集合类的内部实现(只要它实现了 java.lang.Iterable 接口),如果使用 Iterator 来遍历集合中元素,一旦不再使用 List 转而使用 Set 来组织数据,那遍历元素的代码不用做任何修改,如果使用 for 来遍历,那所有遍历此集合的算法都得做相应调整,因为List有序,Set无序,结构不同,他们的访问算法也不一样.
LintCode刷题
·进入指定目录
cd ..返回上一级
·求最大公约数
建立一个for循环,循环小于等于两数较小的值此,当两数都可整除时记录下i值,最后返回一个最后一个i值
public class Main {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int resultint=0;
for (int i=1;i<=b;i++){
if (b%i==0&&a%i==0){
resultint=i;
}
}
System.out.println(resultint);
}
}
·判断3和5的倍数
%判断即可
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int a=scanner.nextInt();
if (a%3==0&&a%5==0){
System.out.println("It's a multiple of three and five");
}else
System.out.println("It's not a multiple of three and five");
}
}
·数组排序
用Arrays.sort便可实现升序,Arrays.toString()来输出(降序则可用字符串的翻转)
import java.util.Arrays;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int num =scanner.nextInt();
int[] arr=new int[num];
for (int i=0;i<num;i++){
arr[i]=scanner.nextInt();
}
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
· 求直角坐标系内两点间距离
两点间距离平方算出来,再用Math.sqrt()来获取开平方的数 保留两位小数 ->(延伸)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int x1=scanner.nextInt();
int y1=scanner.nextInt();
int x2=scanner.nextInt();
int y2=scanner.nextInt();
double temp= Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
System.out.printf("%.2f\n", temp);
}
}
·遍历ArrayList集合
用Iterator类或forEach都可以实现遍历 ->(延伸)
import java.util.ArrayList;
import java.util.Iterator;
public class Solution {
public void traverseList(ArrayList<String> list) {
Iterator<String> iterator=list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
学习时间:
2021-8-12 9:00-11:45、13:30-17:30
学习产出:
刷题*6
学习博客*1