java api (java common classes, collection)

Scanner

Create input stream object

//System.in代表从键盘输入
Scanner sc = new Scanner(System.in);

Get the string entered by the keyboard

String str = sc.next();

Get a number entered by the keyboard

int num = sc.nextInt();

Close input stream

sc.close();

Random

Create an object

Random r = new Random();

Get random int numbers

int num1 = r.nextInt();

Get a random int number with range (left closed and right open)

int num2 = r.nextInt(10); //随机取0~9数字

Calendar

Methods to get or set various calendar fields

Create an object

Calendar c = Calendar.getInstance();

Get the current local time

c.getTime().toLocaleString();

Get the current time with Date

Date time = new Date(System.currentTimeMillis());
new SimpleDateFormat("YYYY-MM-DD hh:mm:ss").format(time);

Array

Array creation

int[] a = new int[10];

Convert array to string

String s = String.valueOf(c);		//c为数组
String s = Arrays.toString(c);		//以集合的形式转换为字符串

Turn the array into a collection

String[] s = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));

Get the length of the array

c1.length		//返回一个整数,注意没有()

Arrays

Arrays is an array tool class.

Convert array to string

String s = Arrays.toString(c);		//以集合的形式转换为字符串

Sort the array in ascending order

Arrays.sort(a);			//a是数组
如果是数值,sort默认按照升序从小到大
如果是字符串,sort默认按照字母的升序
如果是自定义的类型,那么这个自定义的类需要有Comparable或者Comparator接口的支持

Determine whether two arrays are equal

Arrays.equals(a, b)		//需要元素内容和元素顺序都相同才相等

ArrayList

The length of the Array array cannot be changed, and the length of the ArrayList collection can be changed

Create an ArrayList collection

//创建字符串集合
ArrayList<String> list = new ArrayList<>();
//创建整数集合
ArrayList<Integer> list = new ArrayList<>();	//泛型在尖括号内填写,只能写引用类型,不能是基本类型

Output collection

System.out.println(list);

Add string to collection

list.add("要添加进集合的字符串");		//返回一个布尔值提示是否添加成功

Read the string in the collection

list.get(0);		//括号内填第几个元素下标,从0开始 ,返回对应元素

Delete the string in the collection

list.remove(0);		//括号内填第几个元素下标,从0开始,返回被删除元素

Get the collection length

list.size();		//返回集合长度

Iterate over the collection

一:
for (int i = 0; i < list.size(); i++) {
	System.out.println(list.get(i));
}
二:
for (String str:list) {
	System.out.println(str);
}

Math

Math tools

Absolute value

Math.abs(-2.5);		//2.5

Arrangement

Math.ceil(6.9);		//向上取整,7.0
Math.floor(6.9);	//向下取整,6.0

rounding

Math.round(6.9);	//7

Power operation, find the m power of n

Math.pow(3,2);		//9.0

square root

Math.sqrt(9);		//3.0

Random number (0~1)

Math.random()

String

The string effect is the character array chae[ ], but the bottom layer is the byte array byte[]

You can directly click the "Hello" method

The content of the string cannot be changed, and all operations on the string must be returned with a new string.

Create String

//空参构造
String s = new String();
//字符数组创建
char[] c = {'a', 'b', 'c'};
String s = new String(c);
System.out.println(s);		//abc
//字节数组创建
byte[] b = {97,98,99};
String s = new String(b);
System.out.println(s);		//abc

Replacement character

s.replace('o', '*')		//前者是旧字符,后者是新字符

Compare strings

s.equals(s1);			//比较s和s1内容是否相同,一般常量放前面:"abc".equals(s);
s==s1;					//比较s和s1地址值是否相同,可用于比较两个对象是否引用同一实例
s.equalsIgnoreCase(s1)  //和equals一样,但是忽略大小写差别

Intercept string

s.substring(4,10)					//截取字符串函数,包含第4位,不含第10位
s.charAt(0)							//截取下标为0的字符
====================================================================================================
//从字符串下标为4的字符开始到下标为9的字符结束截取字符串到字符数组,第四个是目标数组偏移量
Str1.getChars(4, 10, Str2, 0);

String Str1 = new String("abcdefghijklmn");
char[] Str2 = new char[6];
Str1.getChars(4, 10, Str2, 0);
System.out.println(Str2);		//efghij
====================================================================================================
String[] s1 = s.split("@")			//根据分隔符@截取字符串,用字符串数组接收,()括号中参数为正则表达式

. $ ) ( * + [ ? \ ^ { | 以上十二个特殊字符不能直接使用,要在前面添加\\
String s1 = new String("aa.bb.cc");
String[] s2 = s1.split(".");
for (String s:s2) 
	System.out.println(s);			//没有输出
String[] s3 = s1.split("\\.");
for (String s:s3) 
	System.out.println(s);			//输出aa换行bb换行cc

Conversion of strings and character arrays

char[] c = s.toCharArray();		//字符串转换为字符数组
s.valueOf(c)     				//字符数组转换为字符串,c是字符数组

Concatenate strings at the end to form a new string

s3 = s1.concat(s2)				
s3 = s1+s2;						//结果和上面一样

Remove the leading and trailing spaces of the string

s.trim()

String case conversion

s.toLowerCase()		//转换为小写
s.toUpperCase() 	//转换为大写

Get string length

s.length()

Find the position where a character or substring appears

indexOf('a')			//查找字符或者子串第一次出现的地方,返回对应下标,没有返回-1
lastIndexOf("abc")		//查找字符或者子串是后一次出现的地方,返回对应下标,没有返回-1

Format string

String.format("a:%c",'a').sout;		//输出a:a,类似C语言的输出方法

StringBuffer

StringBuffer constructor:

StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)

Convert to string

sb.toString();

Get the length

sb.length();

Reverse the characters in the StringBuffer object

StringBuffer reverse()

Intercept substring

String substring(int startIndex)
String substring(int startIndex,int endIndex)

Get a character

char charAt(int where)

Replace a character

void setCharAt(int where,char ch)

Intercept a certain string to a character array

void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sb.getChars(0,3,a,0);			//截取下标为0-2的字符串到字符数组a中,数组偏移量为0

Additional character skewer

append()		//追加字符串到stringbuff对象末尾

Insert string

insert()
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)		
StringBuffer insert(int index,Object obj)	//index指定将字符串插入到StringBuffer对象中的位置的下标。

Delete character segment

StringBuffer delete(0,4)				//删除下标为0-3的字符
StringBuffer deleteCharAt(5)      		//删除下标为5的字符

replace

StringBuffer replace(0,2,"abc")		//将下标为0-2的字符串替换为abc

Guess you like

Origin blog.csdn.net/xxmuq/article/details/113284307