常用类的学习
String类
- int length():获取字符串的长度。
String的判断功能:
-
boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
-
boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
-
boolean contains(String str):判断大字符串中是否包含小字符串
-
boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
-
boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
-
boolean isEmpty():判断字符串是否为空。
-
boolean matches() 返回是否匹配指定的字符串
String获取索引:
-
char charAt(int index):获取指定索引位置的字符
-
int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
-
int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
-
int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
-
int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
-
int lastIndexOf():返回指定字符串在此字符串中从指定位置后最后一次出现处的索引。
String截取字符串:
-
String substring(int start):从指定位置开始截取字符串,默认到末尾。
-
String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
String的转换功能:
-
byte[] getBytes():把字符串转换为字节数组。
-
char[] toCharArray():把字符串转换为字符数组。
-
static String valueOf(char[] chs):把字符数组转成字符串。
-
static String valueOf(int i):把int类型的数据转成字符串。
-
String toLowerCase():把字符串转成小写。
-
String toUpperCase():把字符串转成大写。
-
String concat(String str):把字符串拼接。
String的替换功能
-
String replace(char old,char new)指定字符替换
-
String replace(String old,String new)指定字符串替换
-
String trim():去掉开始和最后的空格
String的比较:
-
int compareTo(String str):比较--- 前面-后面
-
int compareToIgnoreCase(String str)比较---忽略大小写
to String()--返回此序列中数据的字符串表现形式
StringBuffer类:
-
大量字符串拼接时建议使用字符串缓冲区(StringBuffer,StringBuilder)效率高
-
int capacity()---获取StringBuffer对象的容量,(默认16,超过容量会自动扩容,
- 扩容--old*2+2)(理论值)
-
int length()---获取字符串缓冲区字符个数(实际值)
-
public StringBuffer(CharSequence seq):构造一个字符缓冲区
-
StringBuffer append(XXX) :把字符串添加到字符缓冲区(可以是任意字符)
- XXX可以不写,可以是boolean,char,char[],String,StringBuffer,float,double,
- Object object,String或者例如("天动万象", 2, 4); // [2,4) 万象等等类型。
-
StringBuffer insert(int offset,XXX):offset是多少,就在多少位后面添加 XXX;
删除
-
StringBuffer delete(int start,int end):删除指定位置的字符 [start,end)[2,4)--2,3
-
StringBuffer deleteCharAt(int index):删除指定下标的字符,
替换
-
StingBuffer replace(int start,int end,String str):用str替换star到end内的字符[start,end)
反转
-
StringBuffer reverse():把字符串反转
截取
-
String substring(int atart):从start截取到末尾
-
String substring(int start,int end):从start截取到end--[start,end)
- ---注意:返回值不再是StringBuffer本身
String与StringBuffer相互转换
String str = "xia"; StringBuffer buffer = new StringBuffer(); //把String转换为StringBuffer buffer.append(str);//一 StringBuffer buffer = new StringBuffer(str)//二 //把StringBuffer转换为String String string = buffer.toString(); String string = buffer.append()+"";
CharSequence接口:char值的一个可读序列
public interface Comparable
-
- public int compareTo(T o);
String,StringBuffer,StringBuilder的区别:
- 答案:StringBuffer,StringBuilder区别:StringBuffer线程安全效率低,StringBuild线程不安全,效率高
- -------String是一个不可变的字符序列,而StringBuffer和StringBuild是一个可以改变的字符序列。
Arrays类里的方法:
- public static String toString(int[] a):打印数组
- public static void sort(int[] a):对数组从小到大排序
- public static void sort(int[] a,int fromindex,int tiindex)对数组a中的fromindex到tiindex排序
- public static int binarySearch(int[] a,int key):二分查找,先要排序,再返回key的下标
- public static int binarySearch(int[] a,int fromindex,int tiindex,int key):
xxxValueOf()与paseInt
-
Integer.parseInt(String s)返回int常量。
-
Integer.valueOf(String s)返回Integer对象
Math类里的方法:
-
public static int abs(int a):取绝对值
-
public static double ceil(double a):向上取整
-
public static double floor(double a):向下取整
-
public static int max(int a,int b) :求a,b最大值 min--求最小值
-
public static double pow(double a,double b):a基数,b指数 a的b次方
-
public static double random():生成随机数
//生成一个[1,20]的随机数 int a = (int) (Math.random() * 20+1);//[0,20)-->[1,21)-->[1,20]
-
public static int round(float a) 参数为double的自学
-
public static double sqrt(double a)
Random类中的方法:
-
public int nextInt()
-
public int nextInt(int n)
int randNumber =rand.nextInt(MAX - MIN + 1) + MIN; // randNumber 将被赋值为一个 MIN 和 MAX 范围内的随机数
//[-10,10] Random random = new Random(); int a = random.nextInt(21)-10;//[0-10,20-10]
System类中的方法:
- 不能实例化
- public static void exit(int status):终止当前java虚拟机
- public static void gc():调用gc通知垃圾回收器回收垃圾,不一定立即执行
- public static void arraycopy()从指定的源数组中复制一个数组,开始在指定的位置,到目标数组的指定位置。
- public static long currentTimeMillis():返回程序执行所需毫秒数
BigInteger类:
- 加(add)减(subtract)乘(multiply)除(divide);
- 位与(and)位或(or)
Date类:
- public long getTime():获取当前时间到1970...毫秒值
- public void setTime(long time):将时间重置到指定毫秒值对应的位置
DateFormat类:
-
日期、时间格式化的抽象类,格式化并解析日期和时间,使用其子类SimpleDatemat
-
SimpleDatemat类继承DateFormat类
-
public final String format(Date date):将日期对象转换成字符串
-
public Date parse(String source):将字符串转成日期对象(格式相同)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a EEE"); //日期转换文本(格式化) Date date1 = new Date(); String str = sdf.format(date1); System.out.println(str);// 2021-12-29 11:20:39 上午 星期三 //String文本转日期 Date date2 = sdf.parse("4020-2-4 12:12:34 下午 星期一");// 字符串格式与解析器格式必须相同 System.out.println(date2);// Tue Feb 04 12:12:34 CST 4020
Calendar类:
-
Calendar类是一个抽象类,使用子类对象GregorianCalendar
-
public static Calendar getInstance():用于创建对象
Calendar c = Calendar.getInstance();
-
public int set(int field,int value):设置一个值给这个字段
c.set(Calendar.YEAR, 2012); System.out.println(c.get(Calendar.YEAR));//2012
-
public int get(int field):输入字段返回所对应的时间日期等
System.out.println(c.get(Calendar.YEAR));//2021 System.out.println(c.get(Calendar.MONDAY));//11----注意:0-11-->1-12月 System.out.println(c.get(Calendar.DATE));//29
-
public void add(int field,int amount):添加或减去指定的时间给定日历,基于日历的规则.
c.add(Calendar.MONTH, 3); System.out.println(c.get(Calendar.MONTH));// 2 System.out.println(c.get(Calendar.YEAR));// 2022
-
public final void set(int year,int month,int date):设置年月日
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~