:::info
🌞Java基础知识常用API,写的简单,仅供复习引起回忆用。
持续更新…
:::
1、其他进制转10进制 .parseInt()
int a = Integer.parseInt("11111",2);
System.out.println(a)
// 将2进制转为10进制
2、十进制转其他进制 .valueOf()
Integer b = Integer.valueOf("444",16); // 使用 16 进制
System.out.println(b); //1092
//可将10进制转换为16进制
3、判断是否是一个字母 Character.isLetter()
System.out.println(Character.isLetter('c'));
System.out.println(Character.isLetter('5'));
4、判断是否是一个数字Character.isDigit()
System.out.println(Character.isDigit('c'));
System.out.println(Character.isDigit('5'));
5、判断是否大写字符 Character.isUpperCase()
6、判断是否小写字符 Character.isLowerCase()
System.out.println(Character.isUpperCase('a')); //False
7、小写字母转大写字母 Character.toUpperCase()
8、大写字母转小写字母 Character.toLowerCase()
System.out.println(Character.toUpperCase('a')); //A
System.out.println(Character.toLowerCase('A')); //a
9.拼接字符串方法
- String1.concat(“xxxx”)
- “xxxxx” + “bbbbb” 常用
"Hello," + " runoob" + "!"
String ss ="abc";
ss.concat("def");
10、返回指定位置的字符 charAt(int index)
String s = "www.runoob.com";
char result = s.charAt(6);
System.out.println(result); //n
11、查找字符出现的位置 indexOf(int ch)
//语法
public int indexOf(int ch )
返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
public int indexOf(int ch, int fromIndex)
返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
int indexOf(String str)
int indexOf(String str, int fromIndex)
//示例
String string = "aaa456ac";
//查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.
System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在
// 从第四个字符位置开始往后继续查找,包含当前位置
System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); 返回结果:6
//(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99
// 从头开始查找是否存在指定的字符
System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7
System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7
//从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。
System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); 返回结果:6
System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); 返回结果:6
12、字符替换 replace(char oldChar, char newChar)
System.out.println(Str.replace('o', 'T'));
13、字符串拆分 split(String regex)
String str = new String("Welcome-to-Runoob");
for (String retval: str.split("-")){
System.out.println(retval);
}
//结果
Welcome
to
Runoob
13、截取指定位置的字符串作为新的字符串 substring(int beginIndex, int endIndex)
String Str = "This is text";
System.out.println(Str.substring(4) ); //is text
System.out.println(Str.substring(4, 10) ); //is te
14、将字符串转换为字符数组 char[] toCharArray()
15、将字符串所有字符转换为小写 toLowerCase()
16、将字符串所有字符转换为大写 toUpperCase()
17、删除字符出头尾的空格 trim()
18、判断字符串是否包含xx contains(CharSequence chars)
String myStr = "Runoob";
System.out.println(myStr.contains("Run")); //true
19、对字符串进行修改 StringBuild (不是线性安全,速度快,常用) StringBuffer
StringBuilder sb = new StringBuilder("abc:");
sb.append("defghj") //字符串追加
System.out.println(sb); //abcdefghj
sb.reverse() //字符串内容反转
System.out.println(sb); //jhgfedcba
sb.delete(0, 1); //删除指定位置的字符
System.out.println(sb); //hgfedcba
sb.insert(1,234) //在指定位置插入 “234”
System.out.println(sb); //h234gfedcba
sb.replace(0, 1, "qqq"); //替换指定位置的字符
System.out.println(sb); //qqq234gfedcba
20、获取当前时间 Date
import java.util.*;
Date date = new Date(); //导的是util里的Date
System.out.println(date); // Sun Dec 11 17:29:50 CST 2022
21、日期时间格式化 SimpleDateFormat
Date date = new Date(); //导的是util里的Date
SimpleDateFormat sf =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //大小写不能错
//MM 是月份,mm 是分;HH 是 24 小时制,而 hh 是 12 小时制。
sf.format(date);
System.out.println(sf.format(date)); //2022-12-11 05:34:05
22、当前时间 日历类Calendar
Calendar cal =Calendar.getInstance(); 初始化
int year =cal.get(Calendar.YEAR);
int month =cal.get(Calendar.MONTH )+1; //月份要+1
int day =cal.get(Calendar.DATE);
int hour = cal.get(Calendar.HOUR_OF_DAY);
//Calendar.HOUR_OF_DAY 24小时制 Calendar.HOUR12小时制
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
System.out.println(year+"-"+month+"-"+day +" "+ hour+":"+minute+":"+second);
//2022-12-11 19:42:15
int week = cal.get(Calendar.DAY_OF_WEEK); //今天是本周的第几天 (今天是周日)
System.out.println(week); //输出1
//获得星期几(注意(这个与Date类是不同的):1代表星期日、2代表星期1、3代表星期二,以此类推)
23、输入 Scanner 通过 Scanner 类来获取用户的输入
通过 Scanner 类来获取用户的输入
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
//以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。可以获得空格。
String str2 =sc.next();
//空格后面的内容不会输出,只能获得空格前的内容
24、ArrayList
//创建对象
ArrayList<Integer> array = new ArrayList<Integer>();
//添加值
array.add(23);
array.add(24);
array.add(25);
System.out.println(array);//[23, 24, 25]
//修改指定的元素
array.set(0, 110);
System.out.println(array);//[110, 24, 25]
//删除指定元素
array.remove(2);
System.out.println(array);//[110, 24]
//计算大小
int x =array.size();
System.out.println(x);
//ArrayList排序
array.sort(null);
System.out.println(array);//[24, 110] Integer类数组排序,由小到大
ArrayList<String> arr = new ArrayList<>();
arr.add("aaa");
arr.add("bbb");
arr.add("ccc");
arr.sort(null);
System.out.println(arr);//[aaa, bbb, ccc] String类,按字母排
24、ArrayList 常用方法
- 判断元素是否存在 .contains()
- 截取部分元素 .subList()
- 判断 此数组中是否包含另一个数组的所有元素 .containsAll()
25、LinkedList
使用Array List情况:
- 频繁访问列表中的某一个元素。
- 只需要在列表末尾进行添加和删除元素操作
使用LinkedList情况
- 只需要在列表末尾进行添加和删除元素操作
- 需要频繁的在列表开头、中间、末尾等位置进行添加和删除元素操作
//创建链表
LinkedList<String> list =new LinkedList<>();
list.add("a");
System.out.println(list);
//元素添加到头部
list.addFirst("q");
System.out.println(list);//[q, a]
26、HashSet
- 不允许有重复元素
- 允许有 null 值
- 无序
- 非线性安全
HashSet<String> hst = new HashSet<>();
hst.add("abc");
hst.add("aaa");
hst.add("aaa");
hst.add("bbb");
System.out.println(hst);//[aaa, abc, bbb]
27、判断元素是否存在
hst.contains("bbb");
28、HashMap
//创建集合
HashSet<String> hst = new HashSet<>();
hst.add("abc");
hst.add("aaa");
hst.add("aaa");
hst.add("bbb");
System.out.println(hst);//[aaa, abc, bbb]
HashMap<Integer, String> hashMap =new HashMap<Integer, String>();
//添加键值对
hashMap.put(0, "a");
hashMap.put(1, "b");
System.out.println(hashMap);
//通过Key访问
System.out.println(hashMap.get(0)); //a
//remove
hashMap.remove(0);
29、HashMap常用方法
hashmap.values();// 查看hashMap 中存在的所有 value 值
replace(); //替换 hashMap 中是指定的 key 对应的 value。
size();
containsValue();//检查 hashMap 中是否存在指定的 value 对应的映射关系。
containsKey(); //检查 hashMap 中是否存在指定的 value 对应的映射关系。