方法
函数/过程,包含了一些代码
语法/结构
修饰词 – 方法返回值类型 – 方法 – (参数列表) – {方法体}
public static — void/int/int[ ]/boolean/String — main/随意(int a, int b)— 一些代码
有返回值的方法, 需要通过return返回结果
return表示方法结束, 没有返回值的方法, 也可以使用return
类的学习方法
-
构造方法 ----- 如何创建对象
-
成员变量 ----- 基本可以忽略
-
成员方法 ----- 功能
Java程序中,默认import的软件包就是 java.lang
常用类
(一). Random:
构造方法 new Random()
1、int nextInt()
产生一个随机整数
// 1.创建Random对象
Random ran = new Random();
// 2.使用方法
int a = ran.nextInt();
2、boolean nextBoolean()
产生一个随机逻辑值
// 产生一个随机boolean值
boolean c = ran.nextBoolean();
3、int nextInt(int n)
产生一个0~n的随机数
// 0~n之间随机数 [0,n)
int b = ran.nextInt(n);
比较Math.random() 和 Random 的使用
1、Math.random() -> 不需要对象,直接调用
Random -> 需要对象
//产生随机数 (0,1]
double i = Math.random();
//产生随机整数 (0,100]
int i1 = (int)(Math.random() * 100);
2、Math.random() -> 返回double, 范围固定0~1
Random中的nextInt() -> 返回int, 范围可指定
(二)String
字符数组可以直接打印, 就是字符串 — JVM自动转换的
char[] arr = {'Y','N','H','C','M'};
//[Y, N, H, C, M]
System.out.println(Arrays.toString(arr));
// 直接打印 YNHCM
System.out.println(arr);
字符串的特点
-
直接赋值的字符串, 都存在字符串常量池中, 优化
-
字符串本身不可变
-
字符串不能用 == 来比较
字符串的构造方法
// 1.简单写法
String s = "hello";
// 2.无参构造器 -> "" 空的字符串
String s1 = new String();
// 3.有参构造 1 -> hello
String s2 = new String("hello");
// 4.有参构造 2 -> hello
char[] chs = {'h','e','l','l','o'};
String s3 = new String(chs);
// 5.有参构造 3 -> ell [起始下标,数量]
String s4 = new String(chs, 1, 3);
// 6.有参构造 4 -> bcde
byte[] bs = {98, 99, 100, 101};
String s5 = new String(bs);
// 7.有参构造 5 -> cde
String s6 = new String(bs, 1, 3);
构造方法的区别
// 构造方式的区别
// new出来的
String s1 = new String("hello");
String s2 = new String("hello");
// 判断s1和s2是否相等
System.out.println(s1 == s2); // false
// 直接赋值
String s3 = "hello";
String s4 = "hello";
System.out.println(s3 == s4); // true
System.out.println(s3 == s1); // false
// 字符串字面量/直接量 的拼接, 出来的还是直接赋值
String s5 = "he" + "llo";
System.out.println(s4 == s5); // true
String s6 = "he";
// 变量和直接量的拼接, 产生一个新的对象
String s7 = s6 + "llo";
System.out.println(s4 == s7); // false
字符串的常用API
String s = "+86hello@qq.com";
String s1 = new String("hello");
// 1.获得某一个下标对应的字符
char c = s.charAt(0);
// 2.字符串比较不能用 s==s1 s1==s
boolean b = s1.equals(s); // s.equals(s1)
// 3.将字符串s, 转换成字符数组
char[] chs = s.toCharArray();
// 4.统计字符串中的字符个数, 长度
int length = s.length();
// 5.判断是否以xx开头 +86
boolean b1 = s.startsWith("+86");
// 6.判断是否以xx结尾 @qq.com
boolean b2 = s.endsWith("@qq.com");
// 7.获得字符串中指定字符第一次出现的位置, 没有字符, 返回-1
int index = s.indexOf('@');
// 8.截取字符串, s本身没变 [起始下标,终止下标) 86he
String sub = s.substring(1, 5);
// 9.字符串翻转
String re = s.reverse();
String s = " \t hello \n ";
// 1.截取字符串, 从指定位置截取到末尾
String sub = s.substring(3);
// 2.是否以指定字符串开始, 开始位置是offset
boolean b = s.startsWith("lo", 3);
// 3.获得指定字符串出现的位置
int index = s.indexOf("qq");
// 4.从指定下标开始往后, 指定字符串第一次出现的位置
int index1 = s.indexOf("qq", 3);
// 5.从指定下标开始往后, 指定字符第一次出现的位置
int index2 = s.indexOf('q', 3);
// 6.忽略大小写比较
boolean t = s.equalsIgnoreCase("Hello");
// 7.去掉字符串左右两边的空白字符
String trim = s.trim();
(三)Scanner
-
int nextInt(): 获取控制台内容, 并且转换成int值返回
-
double nextDouble(): 获取控制台内容,并且转换成double返回
-
String next(): 获取控制台字符串, 并返回(遇到回车或者空格读取结束)
-
String nextLine(): 获取控制台一整行字符串,并返回(遇到回车结束)
Scanner console = new Scanner(System.in);
//输入一个字符串 遇到回车结束
String s1 = console.nextLine();
//输入一个 int 类型的数
int i = console.nextInt();
//输入一个 float 类型的数
float f = console.nextFloat();
//输入一个 double 类型的数
double d = console.nextDouble();
//输入一个字符串 遇到回车或者空格读取结束
String s = console.next();
(四)ArrayList【重要】
用来存储数据, 可变长数组
- ArrayList 仅支持引用类型
基本数据类型都有对应的引用类型
int -> Integer
char -> Character
byte -> Byte
boolean -> Boolean
double -> Double
- 构造方法
空参构造器: 没有内容的空的集合
// <String>
ArrayList<String> list = new ArrayList();
// 打印集合对象, 就是将内容打印
System.out.println(list); // []
- 常用API
//String
ArrayList<String> arrayL = new ArrayList<String>();
//添加
arrayL.add("wm");
arrayL.add("ww");
arrayL.add("mm");
arrayL.add(1,"pp");
System.out.println(arrayL); // [wm, pp, ww, mm]
//删除(下标)
String remove = arrayL.remove(1);
System.out.println(remove); // pp
System.out.println(arrayL); // [wm, ww, mm]
//查找(下标)
String get = arrayL.get(1);
System.out.println(get); // ww
System.out.println(arrayL); // [wm, ww, mm]
//求得数组长度
int size = arrayL.size();
System.out.println(size); //3
//数组遍历/迭代
for(int i = 0; i < arrayL.size(); i++) {
System.out.print(arrayL.get(i) + "\t");
} //wm ww mm
- 泛型
1、规定集合中元素的类型
2、没有规定泛型, 集合中的元素可以是任意类型
3、使用get()之类的方法时, 就不能确定返回值类型
4、使用集合时, 需要规定泛型
有一些类的方法在使用时, 不需要创建对象
java.lang.Math
- double Math.random(): 返回0.0~1.0 之间的随机数
// random() -> [0,1)
double rand = Math.random();
// [0, 100) 整数
int r2 = (int) (Math.random() * 100);
// [65, 90] [0,1)->[0,26)->[65,91)
int r3 = (int) (Math.random() * 26 + 'A');
// 就是随机大写字母
char c = (char) (Math.random() * 26 + 65);
- double Math.pow(double a, double b): 返回 a 的 b 次幂
转义字符: \n:回车 \r:换行 \t:制表符 \\:\本身
// double Math.pow(double, double)
int pow = (int) Math.pow(2, 10);
- 其他
// 求绝对值
int abs = Math.abs(1);
// ceil: 向上取整 -> 比现在这个数大
double ceil = Math.ceil(-3.01);
// floor: 向下取整 -> 比参数要小
double floor = Math.floor(-3.9999999);
// round: 四舍五入 -> 参数 + 0.5 再floor
double round = Math.round(3.51);
// sqrt: 求开方
double sqrt = Math.sqrt(2);
java.util.Arrays
- String Arrays.toString(数组): 以[元素1, 元素2]格式打印数组
int[] arr = {1,2,3,4,5};
//[1, 2, 3, 4, 5]
System.out.println(Arrays.toString(arr));
- int[ ] Arrays.copyOf(int[ ] src, int newLength):数组复制
// 需求: 让arr变成 {1,2,3,4,5,6}
int[] arr = {1,2,3,4,5};
// 参数1: 要复制的数组 -> 源数组
// 参数2: 要生成的新数组的长度
// 返回值: 生成的新数组
arr = Arrays.copyOf(arr, arr.length + 1);
arr[arr.length - 1] = 6;
- void sort(int[ ] arr): 按照升序排序 只支持基本数据类型和String
int[] a = {8, 3, 1, 7, 2};
// 在原来的数组上直接修改排序 - 升序 [1, 2, 3, 7, 8]
Arrays.sort(a);
System.out.println(Arrays.toString(a));
// 字典顺序 [jack, lucy, rose, tom]
String[] ss = {"lucy","tom","jack","rose"};
Arrays.sort(ss);
System.out.println(Arrays.toString(ss));
System
-
void print(内容) -> 打印, 不换行
-
void println(空/内容) -> 打印完, 换行
-
void System.arraycopy:数组赋值
int[] arr = {1,2,3,4,5};
// 1.数组扩容
int[] b = new int[arr.length+1];
// 2.数组复制
/*
参数1: 要复制的数组 - 源数组
参数2: 从要源数组的哪个位置开始复制
参数3: 要复制到哪个数组中 - 目标数组
参数4: 要从b数组的哪个位置开始存放值
参数5: 要复制几个元素
*/
System.arraycopy(arr, 0, b, 0, arr.length);
// 3.再最后加一个6
b[b.length - 1] = 6;
// 4.将arr替换
arr = b;
- long System.currentTimeMillis( ):求时间
// 获得当前系统时间 - 毫秒
long time = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++){
}
long time1 = System.currentTimeMillis();
// 计算代码执行效率(时间差)
System.out.println(time1 - time);