更新了版本,请移步Java常用命令总结 持续更新中!!!
目录
基础输入
保留几位小数
Random
数组
System
Arrays
HashMap
HashSet
String
StringBuilder
ArrayList
Deque栈
Queue队列
PriorityQueue优先队列
常用数学算法&&结论
结论
算法
Scanner
Integer
Iterator 迭代器
Math
Comparator && Comparable 的使用
其他
// 基础输入
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);//输入
}
}
// 非静态方法调用
new Main.Solution();//static函数里面调用非static函数 类.函数
// 更快的输入方式import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
String[] temp = read.readLine().trim().split(" ");
int T = Integer.parseInt(read.readLine());
writer.write(Integer.toString(T));
writer.newLine();
writer.flush();
}
}
scan.hasNext();
...hasNextInt();
...hasNextLine();
...next();
...nextInt;
...nextLine();
double res = 0d;
new DecimalFormat("0.00").format(res);
System.out.printf("%.3f",res);
Random r=new Random();
r.nextInt(a);//[0,a)
new int[0][]//空数组
System.arraycopy(original, i, ans[i / n], 0, n);// 数组复制
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
代码解释:
Object src : 原数组
int srcPos : 从元数据的起始位置开始
Object dest : 目标数组
int destPos : 目标数组的开始起始位置
int length : 要copy的数组的长度
Arrays.sort();// 排序
Arrays.copyOfRange(intersection, 0, index);// 返回复制的数组
Arrays.asList(nums);// 数组转List
Map
map=new HashMap(){ {
put(')','(');
put(']','[');
put('}','{');
}
};//初始化实例
map.get(key);
map.put(key,num);
map.containsKey(key);
map.keyset();//返回key的String[]集合 常用于循环
map.getOrDefault(key,0);
map.remove(key);
map.values();// 返回map的values集合
set.add();
set.remove();
set.size();
set.clear();
set.toArrays(); // hashset转数组
s.trim();//去除前后空格
s.substring(start, start + maxLen)//左闭右开
s.indexOf(ss);//ss可以是整数也可以是字符串 返回指定字符串第一次出现的索引 没有则返回-1也可以从fromIndex开始位置
s.indexOf(ss,fromIndex);
String[] ss = s.split(" ");//字符串按照空格分开
String.valueOf();//其他转成String
String.contains("x");//是否包含
StringBuilder sb=new StringBuilder();
sb.append('(');
sb.deleteCharAt(cur.length()-1);
sb.delete(l,r);
sb.insert(pos,string);
sb.toString();//转换成String
new StringBuilder(s).reverse().toString();//字符串反转
List
> ans = new ArrayList
>();// 两个List的初始化方式
ans.add(new ArrayList
(t));//List t = new ArrayList (); List
list = new ArrayList(); list.add(i,o) // 向list i 位置插入
list.toArray(new int[list.size()][]);// list转数组
Collections.swap(output, first, i);// 把List两个位置的元素调换
Collections.sort(l1,(a,b)->a.compareTo(b));
Collections.sort(l2,(a,b)->a.compareTo(b));
Deque
d =new LinkedList(); d.isEmpty();
d.peek();
d.pop();
d.push();
Queue
q=new LinkedList(); q.offer();
q.element();
q.poll();
//优先队列 弹出最左边数
PriorityQueue
queue = new PriorityQueue<>(new Comparator () {//比较器 升序 @Override
public int compare(ListNode o1, ListNode o2) {
return o1.val-o2.val;
}
});
=============================== 位运算 ===============================
a^a = 0; // 相同数 异或 =0
0^a = a;
n & (n - 1); // 最低位1改为0
n & (-n); // 获取最低位1
1 << n; // 2^n
if (rev < Integer.MIN_VALUE / 10 || rev > Integer.MAX_VALUE / 10) // 判断是否超界
// 欧几里得算法 算最大公约数
public static int gcd(int a, int b) {
return a % b == 0 ? b : gcd(b, a % b);}
Integer.valueOf(s);//String转Integer
Integer.toString(n);//Integer转String
用于迭代 ArrayList 和 HashSet 等集合。
Iterator
it = sites.iterator(); it.next();
it.hasNext() ;
it.remove();
Math.round() ;// 四舍五入
Math.ceil();// 向上取整
Math.cos(Math.PI);// 180度的余弦值
public interface Comparable
{
public int compareTo(T o);
}public interface Comparator
{ public int compare(T lhs, T rhs);
}class Students implements Comparable
{
int age;
String name;
Students(int age,String name){
this.age=age; this.name=name;
}
@Override
//重写compareTo方法按Students的年龄排序
public int compareTo(Students o) {
if (this.age-o.age>0)
return 1;
if (this.age-o.age<0)
return -1;
else
return 0;
}@Override
public String toString() {
return "Students{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}public class TsetCompareable {
public static void main(String args[]){List
list =new ArrayList();
list.add(new Students(10,"zhangsan"));
list.add(new Students(18,"lisi"));
list.add(new Students(9,"wangwu"));
Collections.sort(list);
System.out.println(list);
}
}
class Student {
int age;
String name;
Student(int age,String name){
this.age=age; this.name=name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
//学生比较器
class StudentComparator implements Comparator{ //重写compare方法
@Override
public int compare(Student student, Student student1) {
if (student.age-student1.age>0)
return 1;
if (student.age-student1.age<0)
return -1;
else
return 0;
}
}
public class ComparatorTest{
public static void main(String args[]){List
list =new ArrayList();
list.add(new Student(10,"zhangsan"));
list.add(new Student(18,"lisi"));
list.add(new Student(9,"wangwu"));
Collections.sort(list,new StudentComparator());
System.out.println(list);
}
}
b.toString().equals("0") // 比较
b.mod(b) // 取余
b.add(b) // 相加
b = new BigInteger("1"); // 初始化
int范围 -2^31——2^31-1,即 -2147483648——2147483647 10^-9 ~ 10^9 (算法题注意范围)