Java
基础
输入数据
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入:")
int number = sc.nextInt();
}
private 关键字
- 避免被其他类修改成员变量
- 是一个权限修饰符
- 可以修饰成员(成员变量和成员方法
- 作用是保护成员不被别的类使用以及修改,被
private
修饰的成员只在本类中才能访问
- 被其他类使用,提供相应的操作
- 提供
"get变量名()"
方法,用于获取成员变量的值,方法用 public 修饰 - 提供
"set变量名(参数)"
方法,用于设置成员变量的值,方法用 public 修饰
- 提供
public class Student{
String name; // 可以在其他类中使用
private int age; // 不可以使用,除非使用 set\get()方法;如下:
public void setAge(int a){
if(a < 0 || a > 120){
System.out.println("要修改的年龄有误")
}else{
age = a;
}
}
public void getAge(){
return a;
}
// 当为对象中的方法时,不加 static
public void show(){
System.out.println(name+","+age)
}
}
public class StudentDome{
public static void main(String[] args){
Student s = new Student();
s.name = "Jarry"; // 可不修饰直接进行修改
s.setAge(-30); // 此时可修改值
s.show();
}
}
遍历字符串
public char charAt( int index )
;:返回指定索引处的 char 值,字符串的索引也是从0开始public int length()
:返回次字符串的长度
public class Hello{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串")
String line = sc.nextLine()
for(int i = 0;i < line.length();i ++){
System.out.println(line.charAt(i));
}
}
}
ArrayList 集合
-
ArrayList 集合方法
继承(extends)
// Demo 文件
public class Demo{
public static void main(String[] args){
int age = 35;
Fu f = new Fu();
f.speak(); // 40
Zi z = new Zi();
z.show(); // 30
z.speak(); // 子类调用父类的方法 40
}
}
// Zi.java 文件
public class Zi extends Fu{
public int age = 25;
public Zi(){
super(); // 默认加进去的,不用自己手动写
System.out.println("Zi中无参构造方法");
}
public Zi(int age){
super(14); // 默认加进去的,不用自己手动写,除非传参
System.out.println("Zi中带参构造函数")
}
public void show(){
int age = 30;
// 访问方法中的成员变量 age
System.out.println(age); // 30
// 访问本类中的成员变量 age
System.out.println(this.age); // 25
// 访问父类的成员变量 age
Systme.out.println(super.age); // 40
}
}
// Fu.java 文件
public class Fu{
public int age = 40;
public Fu(){} // 无参构造方法
public Fu(int age){ // 带参构造方法
System.out.println("fu中带参构造方法被调用");
}
public void speak(){
System.out.println(age) // 40
}
}
继承中构造方法的访问特点
- 子类初始化之前,要先完成父类数据的初始化
- 每一个子类构造方法的第一条语句默认都是
super()
- 如果父类中没有无参构造方法,只有带参构造方法,就会出现错误
- ①通过使用
super
关键字取显示的调用父类的带参构造方法 - ②在父类中提供一个无参构造方法
- ①通过使用
包
新建包
修饰符
权限修饰符
public class Person{
private void show1(){}
protected void show2(){}
void show3(){}
public void show4(){}
}
修饰符 | 同一个类中 | 同一个包中子类无关类 | 不同包的子类 | 不同包的无关类 |
---|---|---|---|---|
private | √ | |||
默认 | √ | √ | ||
protected | √ | √ | √ | |
public | √ | √ | √ | √ |
protected
-
定义:protected是受保护的,受到该类所在的包所保护。
-
作用域:被protected所修饰的成员会被位于同一package中的所有类访问到。同时,被protected所修饰的成员也能被该类的所有子类继承下来。(注意:这里是指同一个package或者不同的package中的子类都能访问)
- 子类与基类在同一包中:被声明为 protected 的变量、方法和构造器能被同一个包中的任何其他类访问;
- 子类与基类不在同一包中:那么在子类中,子类实例可以访问其从基类继承而来的 protected 方法,而不能访问基类实例的protected方法。
final
- final 关键字是最终的意思,可以修饰成员方法、成员变量、类
- 特点
- 修饰方法:表名该方法是最终方法,不能被重写
- 修饰变量:表名变量是常量,不能再次被赋值
static (静态)
- static 是静态的意思,可以修饰成员方法,成员变量
package com.item;
public class Student{
public static String name;
public int age;
public void show(){
System.out.println(name+","+age)
}
}
package com.item;
public class Demo{
public static void main(String[] args){
Student.name = "Jarry"; // 默认共享成员变量
Student s = new Student();
s.age = 12;
s.show(); // Jarry,12
Student s2 = "John";
s2.age = 15;
s2.show(); // Jarry,15
}
}
多态
- 对象的多态性:父类的引用指向子类的对象
// public class Man extends Person{}
Person p1 = new Man();
-
多态的使用:虚拟方法调用
-
有了对象的多态性后;在编译期,只能调用父类中已申明的方法,在运行期,实际执行的是子类重写父类的方法。
// Demo public class Demo{ public static void main(String[] args){ Person m1 = new Man(); // 多态 m1.eat(); // The man is eating! m1.walk(); // 没有在父类中申明(定义),不能被调用 } } // 父类 public class Person{ private String name; private int age; public Person(){}; public Person(String name,int age){ this.name = name; this.age = age; } public void eat(){ System.out.println("The person is eating!"); } public void setName(String name){ this.name = name; } public void setAge(int age){ this.age = age; } public String getName(){ return name; } public int getAge(){ return age; } } // 子类 public class Man extends Person{ // 无参构造 public Man(){}; // 有参构造 public Man(String name,int age){ super(name,age); } // 子类私有方法(多态时不能调用) public void walk(){ System.out.println("The man is walking!") } // 父类方法重写 @Override public void eat(){ System.out.println("The man is eating!") } }
-
-
多态的使用前提
- ①类的继承关系
- ②方法的重写
- ③只适用于方法,不适用于属性
抽象类
public class Demo{
public static void main(){
Animal n1 = new Animal();
n1.eat(); // 报错,不能
Animal n2 = new Cat();
n2.eat(); // 如果是抽象方法,只能用多态定义,然后子类重写后才能调用
}
}
// 抽象类 【抽象类中可以有具体方法,也可无抽象方法;但是抽象方法只能定义在抽象类中】
public abstract class Animal{
// 抽象方法
public abstract void eat(); // 注意:不能写方法内容
public void sleep(){
System.out.println("The animal is sleeping!");
}
}
// 子类
// 抽象类的子类需要重写抽象类中的方法
// 或者子类是抽象类
public class Cat extends Animal{
@Override
public void eat(){
System.out.println("子类重写父类的eat方法")
}
}
接口
接口的特点
-
接口用关键字
interface
修饰public interface 接口名 {}
-
子类继承接口时,用
implements
表示(不是用extends
)public class 类名 implements 接口名 {}
-
接口中的变量都是默认静态修饰 (static) 的常量 (final) ,不能被修改
-
一个类中也可实现多个接口
- 语法:
publi class 类名 implements 接口1 , 接口2 {}
- 语法:
-
默认修饰符:
public static final
public interface Inter{ public int num = 10; public static final int num3 = 30; // 接口中的变量默认是此类型,可用以下代码代替 int num3 = 30; // 可用代码代替,且是不能被修改的静态常量 public abstract void eat(); // 抽象方法 void play(); // 在接口中也可这样写,默认 public abstract void play(); public void play(){System.out.println("jjjjj"); } // 错误,在接口中不能写具体方法(只能存在抽象方法) } // 子类 public abstract class ZiInterface extends Object implements Inter{ // extends Object 是默认的,再没有父类的情况下可以省略不写,别忘了abstract @Override // 必须要重写 public void eat(){ System.out.println("The things is eating!") } } // Demo public class Demo{ public static void main(String[] args){ Inter interDemo = new ZiInterface(); interDemo.eat(); // The things is eating! } }
-
内部类
- 第一种
// Outer 类
public class Outer{
private int num = 239;
public void method(){
class Inter{
public void show(){
System.out.pritln(num);
}
}
Inter i = new Inter();
i.show();
}
}
// Demo
public class Demo{
public static void main(String[] args){
Outer o = new Outer();
o.method(); // 239
}
}
-
第二种
public class Outer{ private int num = 34; public class Inter{ public void show(){ System.out.println(num); } } public void method(){ Inter i = new Inter(); i.show(); } } // Demo public class Demo{ public static void main(String[] args){ Outer o = new Outer(); o.method(); } }
-
匿名内部类
// 接口 Inter public interface Inter{ int age = 53; void show(); } // Outer public class Outer{ public void method(){ new Inter(){ @Override public void show(){ System.out.println(age); // 53 System.out.println("The niming is running!") } }.show(); // 当匿名类中有多个重写的方法被调用时还有第二种写法 // Inter i = new Inter(){ // @Override // public void show(){ // System.out.println(age); // } // }; // i.show(); // 53 // i.show(); // 53 } } // Demo public class Demo{ public static void main(String[] args){ Outer o = new Outer(); o.method(); // 53 // The niming is running! } }
冒泡排序
public class Demo{
public static void main(String[] args) {
int[] arr = {55,36,93,59,29};
// System.out.println(Arrays.toString(arr)); // 第一种(利用Array类中的toString方法)
System.out.println("排序前"+arrayTostring(arr)); // 第二种(遍历,并转为字符串)
zhuan(arr); // 转过去的数组还是之前{}中的 arr
System.out.println("排序后"+arrayTostring(arr));
}
// 数组直接时打印不出来的
// 如果要打印,需要遍历为字符串类型;否则为数组的首地址
public static String arrayTostring(int[] arr){
StringBuilder bu = new StringBuilder();
bu.append("[");
for(int i = 0;i < arr.length;i ++){
if(i == arr.length - 1){
bu.append(arr[i]);
}else{
bu.append(arr[i]).append(", ");
}
}
bu.append("]");
String s = bu.toString();
return s;
}
public static void zhuan(int[] arr){
for(int i = 0;i < arr.length-1;i ++) {
for (int j = 0; j < arr.length - 1 - 0; j++) {
int temp;
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
Arrays 类
方法名 | 说明 |
---|---|
public static String toString(int[] a) | 返回指定数组的内容的字符串表示形式 |
public static void sort(int[] a) | 按照数字顺序排列指定的数组(排序) |
public static void main(String[] args){
int[] arr = {43,543,23,56,6};
System.out.println("排序前:"+ Arrays.toString(arr)); // [43, 543, 23, 56, 6]
Arrays.sort(arr);
System.out.println("排序后:"+ Arrays.toString(arr)); // [6, 23, 43, 56, 543]
}
基本类型包装类(int 与 String 相互换)
日期工具类
案例(任意一年的二月有多少天)
public class Demo{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();
Calendar d = Calendar.getInstance();
// 设置日历对象的年、月、日
d.set(year,2,1);
// 3月1日往前推一天就是2月的最后一天
d.add(Calendar.DATE,-1);
// 获取这一天即可
int date = d.get(Calendar.DATE);
System.out.println(year+"年的2月份有"+date+"天");
}
}
异常
自定义异常处理(案例)
// ScoreException.java 文件
public class ScoreException extends Exception{
public ScoreException(){}
public ScoreException(String message){
super(message);
}
}
// Teacher.java 文件
public class Teacher {
public void checkScore(int score) throws ScoreException {
if(score < 0 || score > 100){
throw new ScoreException("请输入0-100之间的数字");
}else {
System.out.println("分数正常");
}
}
}
// Demo.java 文件
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = sc.nextInt();
Teacher t = new Teacher();
try {
t.checkScore(score);
} catch (ScoreException e) {
e.printStackTrace();
}
}
}
Collection 集合
Collection 集合常用方法
方法名 | 说明 |
---|---|
boolean | 添加元素 |
boolean remove(Object o) | 从集合中移除指定的元素 |
void clear() | 清空集合中的元素 |
boolean contains(Object o) | 判断集合中是否存在指定的元素 |
boolean isEmpty() | 判断集合是否为空 |
int size() | 集合的长度,也就是集合中元素的个数 |
-
案例
package Collection; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class StudentDemo { public static void main(String[] args) { Student s1 = new Student("Jarry",23); Student s2 = new Student("John",12); Student s3 = new Student("Zip",13); Collection<Student> c =new ArrayList<Student>(); c.add(s1); c.add(s2); c.add(s3); // 迭代器 Iterator<Student> it = c.iterator(); while(it.hasNext()){ Student s = it.next(); System.out.println("年龄:"+s.getAge()+", 姓名:"+s.getName()); } } }
List 集合
List 集合特有方法
-
案例
- 遍历List集合,如果有 “world ”元素,则添加一个“test”这个元素
public class Demo{ public static void main(String[] args){ List<String> li = new ArrayList<String>(); li.add("Hello"); li.add("world"); li.add("Java"); /* // 迭代器 Iterator<String> it = li.iterator(); while(it.hasNext()){ String s = it.next(); 此步骤会使实际次数增加1 if(s.equals("world")){ li.add("test"); } } */ // 报错信息: /* Exception in thread "main" java.util.ConcurrentModificationException at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1043) at java.base/java.util.ArrayList$Itr.next(ArrayList.java:997) at Collection.StudentDemo.main(StudentDemo.java:35) */ // 出错(查看源代码,其中预期修改次数与实际修改次数不一致,所以报错) // 改进: for(int i = 0;i < li.size();i ++){ String s = li.get(i); if(s.equals("world")){ li.add("test"); } } // 增强 for /* for(String s : li){ if(s.equals("world")){ li.add("test"); } } */ } System.out.println(li); // [Hello, world, Java, test] }
Set 集合
-
特点:
- 不包含重复元素的集合
- 没有带索引的方法,所以不能使用普通for 循环遍历
public static void main(String[] args){ Set<String> set = new HashSet<String>(); set.add("Jarry"); set.add("John"); for(String s :set){ Systme.out.println(s); /* 输出: (顺序不一样) John Jarry */ } }
-
哈希值
-
是JDK根据对象的
地址
或者字符串
或者数字
算出来的int类型的数值 -
Object 类中有一个方法可以获取对象的哈希值
-
public int hashCode():返回对象的哈希值 (同一个对象的哈希值是相同的,不同则哈希值也不同)
Student s = new Student("jarry",12); System.out.println(s.hashCode); // 1060830840 (哈希值)
-
LinkedHashSet 集合概述和特点
-
哈希表和链表实现的Set接口,具有可预测的迭代次序
-
由链表保证元素有序,也就是说元素的存储和取出顺序是一致的
-
由哈希表保证 元素唯一,也就是说没有重复的元素
public static void main(String[] args){ LinkedHashSet<String> link = new LinkedHashSet<String>(); link.add("hello"); link.add("world"); for(String s : link){ System.out.println(s); } /* 输出: (顺序一样) hello world */ }
-
TreeSet 集合(比较器 Comparator)
-
元素有序 按照一定的规则进行排序,具体排序方式取决于构造方法
- TreeSet():根据其元素的自然排序进行排序
- TreeSet(Comparator comparator):根据指定的比较器进行排序
-
没有带索引的方法,所以不能使用普通for 循环遍历
-
由于是Set集合,所以不包含重复元素的集合
-
自然排序 Comparable 的使用(案例)
-
存储学生对象并遍历,创建TreeSet集合使用无参构造方法
-
要求:按照年龄从小到大排序,年龄相同时,按照i姓名的字母顺序进行排序
- 要调用一个Comparable接口,并且要重写一个排序方法
// Student package TreeSet; public class Student implements Comparable<Student> { private int age; private String name; public Student(){} public Student(String name,int age){ this.name = name; this.age = age; } public void setAge(int age){ this.age = age; } public void setName(String name){ this.name = name; } public int getAge(){ return age; } public String getName(){ return name; } @Override public int compareTo(Student s) { // return 0; // 只能返回第一条 // return 1; // 升序排列 // return -1; // 降序排列 // 按照年龄从小到大排序 int num = this.age - s.age; return num; } } // Demo package TreeSet; import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { Student s3 = new Student("Jarry",34); Student s1 = new Student("John",15); Student s2 = new Student("Zip",18); TreeSet<Student> t = new TreeSet<Student>(); t.add(s3); t.add(s1); t.add(s2); System.out.println("按照年龄从小到大排序:"); for(Student s : t){ System.out.println("年龄:"+s.getAge()+", 姓名:"+s.getName()); } } }
-
-
Comparator 比较器的使用
-
用 treeSet 集合存储自定义对象,带参构造方法使用的是比较器排序对元素进行排序的
-
比较器排序,就是让集合构造方法接受收 Comparator 的是实现类对象,重写compare (To1,To2)方法
public static void main(String[] args){ TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){ @Override public int compare(Student s1,Student s2){ int num = s1.getAge() - s2.getAge(); // 如果年龄一样,则用姓名排序 int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num; return num2; } }); Student s3 = new Student("Jarry",34); Student s1 = new Student("John",15); Student s2 = new Student("Zip",18); TreeSet<Student> t = new TreeSet<Student>(); t.add(s3); t.add(s1); t.add(s2); System.out.println("按照年龄从小到大排序:"); for(Student s : t){ System.out.println("年龄:"+s.getAge()+", 姓名:"+s.getName()); } /* 输出结果: 按照年龄从小到大排序: 年龄:15, 姓名:John 年龄:18, 姓名:Zip 年龄:34, 姓名:Jarry */ }
-
-
案例
-
生成10个1-20之间的随机数,并且添加到集合中
public static void main(String[] args){ Set<Integer> set = new HashSet<Integer>(); // 产生的随机数是乱序的 Set<Integer> set = new TreeSet<Integer>(); // 产生的随机数是进经过排序的 // 创建随机数对象 Random r = new Random(); // 判断集合的长度是否小于10 while(set.size()<10){ // 产生一个随机数,并添加到集合中 int number = r.nextInt(20) + 1; // 括号中的数字代表:1-20之间的数字 set.add(number); } for(Integer I : set){ System.out.println(i); } }
-
泛型
// Generic.java
/* 第一种表示方法:
public class Generic <T>{
public T getT(){
return t;
}
public void setT(T t){
this.t = t;
}
}
*/
// 第二种:
public class Generic {
public <T> void show(T t){
System.out.println(t);
}
}
// Demo
public class Demo{
public static void main(String[] args){
Generic g = new Generic();
g.show("Hello");
g.show(23);
g.show(true);
g.show(12.34);
}
}
类型通配符
可变参数
Math集合
public class Demo{
public static void main(String[] args){ // 数据都具有唯一性
Map<String,String> map = new HashMap<String,String>();
map.put("hello","world");
System.out.println(map); // {Hello=world}
}
}
-
Map 集合的基本功能
方法名 说明 V put(K key,V value) 添加元素 V remove(Object key) 根据键删除值对元素 void clear() 移除所有的键值对元素 boolean containsKey(Object key) 判断集合是否包含指定的键 boolean containsValue(Object key) 判断是否包含指定的值 boolean isEmpty() 判断集合是否为空 int size() 集合的长度,也就是集合中键值对的个数 public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("Hello","World"); map.put("Map","HashMap"); System.out.println(map); // {Hello=World, Map=HashMap} System.out.println(map.remove("Hello")); // World System.out.println(map.remove("Test")); // null System.out.println(map); // {Map=HashMap} System.out.println(map.containsKey("Map")); // true map.clear(); System.out.println(map); // {} System.out.println(map.containsKey("Map")); // false }
-
Map 集合的获取功能
public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("Hello","World"); map.put("Map","HashMap"); // get(Object key) : 根据键获取值 System.out.println(map.get("Hello")); // World // Set<K> keySet() :获取所有键的集合 Set<String> keySet = map.keySet(); for(String key : keySet){ System.out.println(key); // Hello Map } // Collection<V> values() : 获取所有值的集合 Collection<String> values = map.values(); for(String value : values){ System.out.println(value); // World HashMap } }
-
File 类
public class myFile{
public static void main(String[] args){
File f1 = new File("myFile\\java.txt");
// 删除当前模块目录下的 java.txt 文件
System.out.println(f1.delete()); // true
// 在当前模块下创建 itcast 目录
System.out.println(f1.mkdir()); // true
// 删除当前模块目录下的 itcast 目录
System.out.println(f1.delete()); // true
// 在当前模块下创建一个目录 itcast ,然后在该目录下创建一个文件 java.txt
File f3 = new File("myFile\\itcast");
System.out.println(f3.mkdir()); // true
File f4 = new File("myFile\\itcast\\java.txt");
System.out.println(f4.createNewFile()); // true
}
}
递归
- 基础案例
public class DiGui{
public static void main(String[] args){
// 不死神兔问题,求第20个月兔子的对数
// 每个月兔子的对数:1,1,2,3,5,8
int[] arr = new int[20];
arr[0] = 1;
arr[1] = 1;
for(int i = 2;i < arr.length; i++){
arr[i] = arr[i - 1] + arr[i - 2];
}
System.out.println(arr[19]); // 6765
// 递归方法
f(20); // 6765
}
public static int f(n){
if(n == 1 || n ==2){
return 1;
}else {
return f(n - 1) + f(n - 2);
}
}
}
-
阶乘案例
// 阶乘:一个正整数的阶乘是所有小于及等于该数的正整数的积,自然数n的阶乘写作n! // 5! = 5*4*3*2*1 // 在方法内部判断该变量的值是否是1 // 是:返回1 // 不是:返回 n*(n -1) public class DiGui{ public static void main(String[] args){ int result = jc(5); System.out.println("5的阶乘为:"+ result); } public static int jc(int n){ if(n == 1){ return 1; }else { return n*jc(n - 1); } } }
字节流写入文件
public class Zijie{
public static void main(String[] args) throw FileNotFoundException {
// FileOutputStream(String name):创建文件输出流以指定的名称写入文件
FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt");
// void write(byte[] b):将b.length 字节从指定的字节数组写入此文件输出流
byte[] bys = {97,98,99,100,101};
}
}
// fos.txt 文件
abcde
-
参照以上案例,文件目录截图