Java(方法、 API [应用程序接口---类] )

方法

函数/过程,包含了一些代码

语法/结构
修饰词 – 方法返回值类型 – 方法 – (参数列表) – {方法体}
public static — void/int/int[ ]/boolean/String — main/随意(int a, int b)— 一些代码

有返回值的方法, 需要通过return返回结果
return表示方法结束, 没有返回值的方法, 也可以使用return

类的学习方法

  1. 构造方法 ----- 如何创建对象

  2. 成员变量 ----- 基本可以忽略

  3. 成员方法 ----- 功能

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. 直接赋值的字符串, 都存在字符串常量池中, 优化

  2. 字符串本身不可变

  3. 字符串不能用 == 来比较

字符串的构造方法

// 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

  1. int nextInt(): 获取控制台内容, 并且转换成int值返回

  2. double nextDouble(): 获取控制台内容,并且转换成double返回

  3. String next(): 获取控制台字符串, 并返回(遇到回车或者空格读取结束)

  4. 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
  1. 构造方法
    空参构造器: 没有内容的空的集合
// <String>
ArrayList<String> list = new ArrayList();
// 打印集合对象, 就是将内容打印
System.out.println(list); // []
  1. 常用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. 泛型
    1、规定集合中元素的类型
    2、没有规定泛型, 集合中的元素可以是任意类型
    3、使用get()之类的方法时, 就不能确定返回值类型
    4、使用集合时, 需要规定泛型

有一些类的方法在使用时, 不需要创建对象

java.lang.Math

  1. 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);
  1. double Math.pow(double a, double b): 返回 a 的 b 次幂
    转义字符: \n:回车 \r:换行 \t:制表符 \\:\本身
// double Math.pow(double, double)   
int pow = (int) Math.pow(2, 10);
  1. 其他
// 求绝对值
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

  1. String Arrays.toString(数组): 以[元素1, 元素2]格式打印数组
int[] arr = {1,2,3,4,5};
//[1, 2, 3, 4, 5]
System.out.println(Arrays.toString(arr));
  1. 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;
  1. 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

  1. void print(内容) -> 打印, 不换行

  2. void println(空/内容) -> 打印完, 换行

  3. 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;
  1. long System.currentTimeMillis( ):求时间
// 获得当前系统时间 - 毫秒
long time = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++){
    
}
long time1 = System.currentTimeMillis();
// 计算代码执行效率(时间差)
System.out.println(time1 - time);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值