概述:
最近這個一個月也是在備考藍(lán)橋杯了,雖然知道有很多不足的地方,算法對我學(xué)大數(shù)據(jù)的來說也很難,但是還是要努力一下,試一試,也算是再重溫和學(xué)習(xí)一下java吧,考前了,也刷不進(jìn)去題了,干脆把a(bǔ)pi寫一下,所以此篇記錄備考刷題過程中,JAVA那些遺漏或者忘記的api包括經(jīng)典算法語句。
API:
1.進(jìn)制轉(zhuǎn)換
一、其他進(jìn)制轉(zhuǎn)十進(jìn)制
1.使用Integer.parseInt()方法
// 2,8,16進(jìn)制轉(zhuǎn)換成10進(jìn)制
// 使用Integer.parseInt()方法;轉(zhuǎn)換成負(fù)數(shù)只需要在字符串前面加上負(fù)號-;大于Integer.MAX_VALUE或小于Integer.MIN_VALUE的轉(zhuǎn)換將會拋出錯誤。
int a = Integer.parseInt("1001",2);//2進(jìn)制轉(zhuǎn)10進(jìn)制
int b = Integer.parseInt("12", 8);? ? // 8進(jìn)制轉(zhuǎn)10進(jìn)制
int c = Integer.parseInt("123ABC", 16);? ? // 16進(jìn)制轉(zhuǎn)10進(jìn)制
2.使用Integer.valueOf()方法
/ 2,8,16進(jìn)制轉(zhuǎn)換成10進(jìn)制
// 使用Integer.valueOf()方法;轉(zhuǎn)換成負(fù)數(shù)只需要在字符串前加上負(fù)號-;大于Integer.MAX_VALUE或小于Integer.MIN_VALUE的轉(zhuǎn)換將會拋出錯誤。
Integer a = Integer.valueOf("1001", 2); // 2進(jìn)制轉(zhuǎn)10進(jìn)制
Integer b = Integer.valueOf("12", 8);? ? // 8進(jìn)制轉(zhuǎn)10進(jìn)制
Integer c = Integer.valueOf("123ABC", 16);? ? // 16進(jìn)制轉(zhuǎn)10進(jìn)制
二、十進(jìn)制轉(zhuǎn)換其他進(jìn)制
1.使用Integer.toxxxString()方法
// 10進(jìn)制轉(zhuǎn)2,8,16進(jìn)制,返回類型String
String tb = Integer.toBinaryString(9);? ? // 10進(jìn)制轉(zhuǎn)2進(jìn)制
String to = Integer.toOctalString(10);? ? // 10進(jìn)制轉(zhuǎn)8進(jìn)制
String th = Integer.toHexString(1194684);? ? // 10進(jìn)制轉(zhuǎn)16進(jìn)制
2.使用Integer.toString() 方法
// 10進(jìn)制轉(zhuǎn)2,8,16進(jìn)制,返回類型String
// 使用Integer.toString()方法;
String tb = Integer.toString(9, 2); // 10進(jìn)制轉(zhuǎn)2進(jìn)制
String to = Integer.toString(10, 8);? ? // 10進(jìn)制轉(zhuǎn)8進(jìn)制
String th = Integer.toString(1194684, 16);// 10進(jìn)制轉(zhuǎn)16進(jìn)制
三、大數(shù)的轉(zhuǎn)換
// 使用BigInteger對象的toString方法;10進(jìn)制轉(zhuǎn)2,8,16進(jìn)制,返回類型String
BigInteger b = new BigInteger("1001", 2); // 2進(jìn)制轉(zhuǎn)10進(jìn)制
BigInteger o = new BigInteger("12", 8);? ? // 8進(jìn)制轉(zhuǎn)10進(jìn)制
BigInteger h = new BigInteger("123ABC", 16);// 16進(jìn)制轉(zhuǎn)10進(jìn)制
String tb = b.toString(2);? ? // 10進(jìn)制轉(zhuǎn)2進(jìn)制
String to = o.toString(8);? ? // 10進(jìn)制轉(zhuǎn)8進(jìn)制
String th = h.toString(16);? ? // 10進(jìn)制轉(zhuǎn)16進(jìn)制
2.String類
s1=abc;
s2=Abc;
s1.equals(s2);//將s1與s2的內(nèi)容進(jìn)行比較,返回一個布爾值(相同為true)
s1.equalsIgnoreCase(s2); //將s1的內(nèi)容與s2的內(nèi)容進(jìn)行比較,不考慮大小寫,返回一個布爾值
char[]c=s1.toCharArray();//將字符串轉(zhuǎn)換為字符數(shù)組
s1.charAt(1);//返回指定索引出的char值
String result=s1.substring(2);//從index處截取到結(jié)尾
String result=s1.substring(0,2);//傳兩個參數(shù):從beginIndex開始截取到endIndex(包含beginIndex,不包含endIndex)
String result=s1.replace("abc","cba");//字符串替換
3.StringBuilder類
構(gòu)造方法:
StringBuilder sb=new StringBuilder();//空字符串
StringBuilder sb=new StringBuilder("abc");
API:
sb.append("紅色").append("藍(lán)色");//追加
sb.reverse();//反轉(zhuǎn)
sb.tostring();//轉(zhuǎn)為string字符串
4.Math工具類(相對重要)
int a=-123;
int b=456;
double c= 123.4;
Math.abs(a);//獲取絕對值
Math.ceil(c);//向上取整
Math.floor(c);//向下取整
Math.round(c);//四舍五入
Math.max(a,b);//獲取兩個int中較大的
Math.min(a,b);//獲取兩個int中較小的
Math.pow(a,b);//返回a的b次冪(double類型)
5.Arrays工具類
需要導(dǎo)入工具包import java.util.*;
int[] arr1 = {11, 22, 33, 44, 55};
int[] arr2 = {11, 22, 33, 44, 66};
Arrays.toString(arr1);// 將數(shù)組元素拼接為帶有格式的字符串
Arrays.equals(arr1, arr2);// 比較兩個數(shù)組內(nèi)容是否相同
Arrays.binarySearch(arr1,33);// 查找元素在數(shù)組中的索引
Arrays.sort(nums); // 對數(shù)組進(jìn)行默認(rèn)升序排序