10 常用类 API
文档下载
jdk 的 API文档 可以百度中文版下载
01 scanner类
package API;
import java.util.Scanner; //导包
/*
Scanner类可以实现键盘输入数据到程序当中
引用类型的一般使用步骤
1.导包 import 包路径.类名称;
如果使用的目标类,和当前类位于同一个包下,则可以省略导包语句不写
只有java.long包下的内容不需要导包,其他的包都需要import语句
2.创建
类名称 包对象 = new 类名称();
3.使用
对象名.成员方法名()
从键盘获取一个数字 int num = sc.nextInt();
从键盘获取一个字符串 String str = sc.next();
*/
public class Demo01Scanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//创建。 system。in表示从键盘输入
int num = sc.nextInt();
System.out.println("the number is " + num);
String str = sc.next();
System.out.println("the string is " + str);
}
}
02匿名对象
package API.Demo02;
import java.util.Scanner;
public class Anonymous02 {
public static void main(String[] args) {
//普通使用方式
// Scanner scanner = new Scanner(System.in);
// System.out.println("scanner a number");
// int num = scanner.nextInt();
//匿名对象的方式
// int num = new Scanner(System.in).nextInt();
// System.out.println("输入的是" + num);
//使用一般写法传入参数
// Scanner sc = new Scanner(System.in);
// methodParam(sc);
//使用匿名对象传参
// methodParam(new Scanner((System.in)));
Scanner sc = methodReturn();
int num = sc.nextInt();
System.out.println("输入的是" + num);
}
public static void methodParam(Scanner sc){
int num = sc.nextInt();
System.out.println("输入的是" + num);
}
public static Scanner methodReturn(){
// Scanner sc = new Scanner(System.in);
// return sc;
return new Scanner(System.in);
}
}
03Random类
package API.Random;
import java.util.Random;
/*
Random用来生成随机数
1.导包 import java.util.Random
2.创建 Random r = new Random();小括号当中留空即可
3.使用 获取一个int数字(范围是int所有范围,有正负两种);int num = r.nextInt()
获取一个随机的int数字(参数代表了范围,左闭右开): int num = r.nextInt(3)
实际上代表的含义是:【0,3),也就是0~2
*/
public class Demo01 {
public static void main(String[] args) {
Random r = new Random();
int num = r.nextInt();
System.out.println("随机数是" + num);
}
}
package API.Random;
import java.util.Random;
public class Demo02 {
public static void main(String[] args) {
Random r = new Random();
for (int i = 0;i<100;i++){
int num = r.nextInt(10);
System.out.println(num);
}
}
}
04数组
对象数组
package API.Array;
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package API.Array;
/*
题目:
定义一个数组,用来存储3个Person对象。
数组有一个缺点;一旦创建,程序运行时间长度不可发生改变
*/
public class Demo01 {
public static void main(String[] args) {
//首先创造一个长度为3的数组,里边用来存放Person类型的对象
Person[] array = new Person[3];
Person one = new Person("迪丽热巴",18);
Person two = new Person("古力娜扎",28);
Person three = new Person("马儿扎哈",38);
//将one当中的地址赋值到数组的0号元素位置
array[0] = one;
array[1] = two;
array[2] = three;
System.out.println(array[0]);//地址值
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[1].getName());//古力娜扎
}
}
ArrayList集合
package API.Array;
import java.util.ArrayList;
/*
数组的长度不可以改变
ArrayList集合的长度可以发生改变
对于ArrayList来说,<E>代表泛型
泛型,也就是说装在集合中的所有元素,全都是统一的什么类型
注意,泛型只能是引用类型,不能是基本类型
注意事项:对于arraylist集合来说,直接打印得到的不是地址值,而是内容。
如果内容为空,得到的是空的中括号:[]
*/
public class Demo02ArrayList {
public static void main(String[] args) {
//创建了一个ArrayList集合,集合的名字叫list,里边装的哦都是String类型的数据
ArrayList<String> list = new ArrayList<>();
System.out.println(list);
//向集合中添加一些数据,需要用到add方法
list.add("赵丽颖");
System.out.println(list);
list.add("古力娜扎");
list.add("迪丽热巴");
System.out.println(list);
}
}
ArrayList常用方法
package API.Array;
import java.util.ArrayList;
/*
常用方法:
public boolean add(E e):向集合中添加元素,参数的类型和泛型一致
备注:对于AraayList集合来说,add添加动作一定成功,返回值可用可不用
对于其他集合(今后学习)来说,add添加动作不一定成功
public E get(int index): 从集合当中获取元素,参数是索引编号,返回值就是对应位置的元素
public E remove(int index):从集合中删除元素,参数是索引编号,返回值就是被删除的元素
public int size(): 获取集合的尺寸长度,返回值是集合中包含的元素个数
*/
public class ArrayListMethod {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
System.out.println(list);//[]
//添加元素
boolean success = list.add("刘玥");
System.out.println(list);
System.out.println("if ok? " + success);
list.add("高圆圆");
list.add("野老师");
System.out.println(list);
//从集合中获取元素
String name = list.get(2);
System.out.println("2's name is " + name);
//删除元素
String whoremoved = list.remove(2);
System.out.println(list);
System.out.println("removed name is " + whoremoved);
//长度
int size = list.size();
System.out.println(size);
//遍历数组
for (int i = 0;i < list.size();i++){
System.out.println(list.get(i));
}
}
}
ArrayList集合存储基本数据
package API.Array;
import java.util.ArrayList;
/*
如果希望像集合里存储基本类型数据,必须使用基本类型对应的“包装类”
基本类型 包装类
byte Byte
short Short
int Integer
ling Long
double Double
char Character
boolean Boolean
自动装箱: 基本类型---->包装类型
自动拆箱: 包装类型---->基本类型
*/
public class ArrayListBasic {
public static void main(String[] args) {
ArrayList<String> listA = new ArrayList<>();
//错误写法! 泛型只能是引用类型,不能是基本类型
// ArrayList<int> listB = new ArrayList<>();
ArrayList<Integer> listC = new ArrayList<>();
listC.add(100);
listC.add(200);
System.out.println(listC);
int num = listC.get(1);
System.out.println("the 1s num is " + num);
}
}
05 String类
字符串创建的方法
package API.String;
/*String一旦创建,不可改变,所以字符串可以共享使用
字符串效果上相当于char[]字符数组,但底层原理是byte[]字节数组
创建字符串的3+1种方式
三种构造方法,
public String(),创建一个空包字符串,不含有内容
public String(char[] array),根据字符数组的内容,来创建对应的字符串。
public String(byte[] array),根据字节的内容,创建对应的字符串
直接创建
String str = hello";
注意: 直接写上双引号,就是字符串
*/
public class Demo01 {
public static void main(String[] args) {
//使用空参构造
String str1 = new String();
System.out.println("第一个字符串是" + str1);
//根据字符数组
char[] charArray = {'A','B','C'};
String str2 = new String(charArray);
System.out.println("第二个字符串是 " + str2);
//根据字节数组
byte[] byteArray = {97,98,99};
String str3 = new String(byteArray);
System.out.println("第三个字符串是 " + str3);
}
}
字符串常量池
package API.String;
/*
只有双引号字符串,才在字符串常量池中
对于基本类型来说, ==是进行数值的比较
对于引用类型来说, ==是进行地址值的比较
*/
public class Demo02 {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "abc";
char[] charArray = {'a','b','c'};
String str3 = new String(charArray);
System.out.println(str1 == str2);
System.out.println(str1 == str3);
System.out.println(str2 == str3);
}
}
字符串比较方法
package API.String;
/*
== 是对对象的地址值比较,如果对字符串的内容比较,可以使用两个方法;
public boolean equals(Object obj);参数可以是任何对象,只有参数是一个字符串并且内容相同的才会true,否则false
public boolean equalsIgnoreCase(String str);忽略大小写进行内容比较
备注:任何对象都可以用Object进行接收
*/
public class Demo03 {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "hello";
char[] charArray = {'h','e','l','l','o'};
String str3 = new String(charArray);
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str3));
System.out.println("hello".equals(str3));//推荐 , 反过来不推荐(反过来如果 str3是null,会报错)
System.out.println("HELLO".equalsIgnoreCase(str1));//忽略大小写
}
}
字符串的获取方法
package API.String;
/*
public int length();获取字符串中含有的字符个数,拿到字符串的长度
public String connect(String str); 将当前字符串和参数字符串拼接成返回值新的字符串
public char charAt(int index); 获取指定索引位置的单个字符(索引从0开视频
public int indexOf(String str); 查找参数字符串在本字符串中首次出现的索引位置,如果没有返回 -1
*/
public class Demo04 {
public static void main(String[] args) {
int length = "fbuygfeleifia".length();
System.out.println(length);
//拼接字符穿
String str1 = "Hello";
String str2 = "world";
String str3 = str1.concat(str2);
System.out.println(str3);
//指定索引的字符
char ch = "Hello".charAt(1);
System.out.println(ch);
//查找参数字符串在本来字符串中出现的第一次索引位置
String original = "Helloworld";
int index = original.indexOf("llo");
System.out.println(index);
}
}
字符串的截取方法
package API.String;
/*
字符串的截取方法
public String substring(int index): 截取从参数位置 一直到字符串末尾,返回新字符串。
public String substring(int begin, int end): 截取一个范围(左闭右开)
*/
public class Demo05 {
public static void main(String[] args) {
String str1 = "Hello world";
String str2 = str1.substring(3);
String str3 = str1.substring(1,4);
System.out.println(str2);
System.out.println(str3);
}
}
字符串的转换
package API.String;
/*
String当中与转换有关的方法
public char[] toCharArray() 将当前字符串拆分成字符数组作为返回值
public byte[] getBytes() 获得当前字符串底层的字节数据
public String replace(CharSequence oldString, CharSequendce newString)
将所有出现的老字符串替换为新的字符串,返回替代之后的结果为新字符串
*/
public class Demo06 {
public static void main(String[] args) {
//转换成为字符数组
char[] chars = "Hello".toCharArray();
System.out.println(chars[0]);
System.out.println(chars.length);
//转换为字节数组
byte[] bytes = "abc".getBytes();
for (int i = 0;i<bytes.length;i++){
System.out.println(bytes[i]);
}
System.out.println("============");
//字符串的内容替换
String str1 = "how do you do";
String str2 = str1.replace("o","*");
System.out.println(str1);
System.out.println(str2);
}
}
字符串的分割
package API.String;
/*
字符串的分割
public String[] split(String regex)
注意: split方法的参数其实是一个正则表达式
今天要注意,如果要按照英文的句号”.“,来切分,必须写”//.“
*/
public class Demo07 {
public static void main(String[] args) {
String str1 = "aaa,bbb,ccc";
String[] array1 = str1.split(",");
for (int i = 0;i < array1.length;i++){
System.out.println(array1[i]);
}
System.out.println("=============");
String str2 = "aaa bbb ccc";
String[] array2 = str1.split(" ");
for (int i = 0;i < array2.length;i++){
System.out.println(array2[i]);
}
}
}
06 Static类
关键字修饰成员1
package API.Static;
public class Student {
private int id;
private String name;
private int age;
static String room;
private static int idCounter;//学号计数器,每当new一个新的对象,计数器++
public Student(){
this.id = ++idCounter;
}
public Student(String name, int age){
this.name = name;
this.age = age;
this.id = ++idCounter;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package API.Static;
/*
如果一个成员变量使用了Static关键字,那么这个变量不再属于对象自己,而是属于所在的类。多个对象共享一份数据
*/
public class Demo01 {
public static void main(String[] args) {
Student one = new Student("郭靖",19);
one.room = "101教室";
System.out.println("姓名:" + one.getName()
+", 年龄:"+one.getAge()+",教室: "+one.room
+",学号: "+ one.getId());
Student two = new Student("黄蓉",16);
System.out.println("姓名:" + two.getName()
+", 年龄:"+two.getAge()+",教室: "+one.room
+",学号: "+ two.getId());
}
}
关键字修饰成员2
package API.Static;
public class MyClass {
public void method(){
System.out.println("这是一个普通的成员方法");
}
public static void methodStatic(){
System.out.println("这是一个静态方法");
}
}
package API.Static;
/*
一旦使用static修饰成员方法,那么这就成为了静态方法
静态方法不属于对象,而是属于类的
如果没有static关键字,那么必须首先创建对象,然后通过对象才能使用他
如果有static关键字,那么不需要创建对象,直接就能通过类名称来使用他
无论是成员对象,还是成员方法。如果有了static,都推荐使用类名称进行调用
静态变量: 类名称.静态变量
静态方法: 类名称.静态方法()
注意:
静态不能直接访问非静态
原因:内存中现有的静态内容,后又的非静态内容
静态方法中不能用this
原因:this代表当前对象,通过谁调用的方法,谁就是当前的对象
*/
public class Demo02 {
public static void main(String[] args) {
MyClass obj = new MyClass();//首先创建对象
//然后才能使用没有static关键字的内容
obj.method();
//对于静态方法而言,可以通过对象名进行调用,也可以直接通过类名来调用
obj.methodStatic();// 正确 不推荐,这种方法在编译之后也会被javac翻译成”类名称.静态方法名“
MyClass.methodStatic();// 正确 推荐
//对于本来当中的静态方法,可以省略类名称
mymethod();
Demo02.mymethod();//和上一行完全等效
}
public static void mymethod(){
System.out.println("this is my mythod");
}
}
07Arrays工具类
数组工具类
package API.Arrays;
import java.util.Arrays;
/*
java.util,Arrays是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组常见的操作。
public static String toString(数组);将参数数组编程字符串(默认格式:[元素1,元素2,...]
public static void sort(数组);按照从小到大对元素进行排序
备注:
如果是数值,默认升序
如果是字符串,默认按照字母升序
如果是自定义的类型,那么这个自定义的类需要有Comparavle或者Comparator接口的支持
*/
public class Demo01 {
public static void main(String[] args) {
int[] intArray = {10, 20, 30};
//将int[] 数组按照默认格式变成字符串
String intStr = Arrays.toString(intArray);
System.out.println(intStr);
int[] array1 = {2, 14, 1, 32, 3};
Arrays.sort(array1);
System.out.println(Arrays.toString(array1));
String[] array2 = {"aaa", "bbb", "ccc"};
Arrays.sort(array2);
System.out.println(Arrays.toString(array2));
}
}
08Math
package API.Math;
/*
java.util.Math提供了大量的静态方法
public static double abs(douvle num); 获取绝对值
public static double ceil(double num); 向上取整
public static double floor(double num);向下取证
public static long round(double num); 四舍五入
*/
public class Demo01 {
public static void main(String[] args) {
System.out.println(Math.abs(-3.14));
System.out.println(Math.ceil(3.1));
System.out.println(Math.floor(3.4));
System.out.println(Math.round(3.4));
}
}
09Calendar类
package API.Calendar;
import java.util.Calendar;
/*
java.util.Calendar类:日历类
Calendar类是一个抽象类,里边提供了很多操作日历字段的方法(Year,Month,Day——Of——Month,Hour)
Calendar类无法直接创建对象使用,里边有一个静态方法叫getinstance(),该方法返回Calendar类的子类对象
static Calendar getInstance() 使用默认时区和语言环境获得一个日历
*/
public class Demo01 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
System.out.println(c);
}
}
package API.Calendar;
import java.util.Calendar;
public class Demo01 {
public static void main(String[] args){
System.out.println((Calendar.getInstance()).MONTH);
}
}
10 System类
package API.System;
/*
java.Lang.System类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有:
public static Tong currentTimeMillis():返回以毫秒为单位的当前时间。
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length):将数组中指定的数据拷贝到另一个数组中。
*/
public class Demo01 {
public static void main(String[] args) {
demmo01();
}
//public static currentTimeMillis();返回以毫秒为单位的当前时间
private static void demmo01(){
long l = System.currentTimeMillis();
for (int i = 1; i < 9999; i++) {
System.out.println(i);
}
//程序执行后,获取一次毫秒值
long l1 = System.currentTimeMillis();
System.out.println(l);
System.out.println(l1);
System.out.println(l1-l);
}
}
11 基本类型包装类
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~