import java.util.*;
import java.util.stream.IntStream;
public class Help {
/**
* 數學計算
*/
public static void mathHelp() {
System.out.println("\n\nmathHelp:--------------------------\n");
System.out.println(Math.random()); // Math.random()返回double[0.0,1.0)
System.out.println(Math.min(1, 2));
System.out.println(Math.max(1, 2));
System.out.println(Math.abs(1));
System.out.println(Math.abs(-1));
System.out.println(Math.floor(1.2)); // 靠近x的最小整數
System.out.println(Math.floor(-1.2)); // 靠近x的最小整數
System.out.println(Math.ceil(1.2)); // 靠近x的最大整數
System.out.println(Math.ceil(-1.2)); // 靠近x的最大整數
System.out.println(Math.round(1.4)); // 四捨五入
System.out.println(Math.round(1.5)); // 四捨五入
}
/**
* 數字
*/
public static void numberHelp() {
System.out.println("\n\nnumberHelp:--------------------------\n");
// 生成隨機數[0,9]
Random r = new Random();
int a = r.nextInt(10); // 注意不包含後邊界
System.out.println(a);
a = (int) (Math.random() * 10); // Math.random()返回double[0.0,1.0)
System.out.println(a);
// 隨機生成一個長度爲100~200位的大數
StringBuffer s = new StringBuffer().append(r.nextInt(8) + 1);
int len = r.nextInt(200);
for (int i = 0; i < len; i++) {
s.append(r.nextInt(10));
}
System.out.println(s.toString());
System.out.println(s.length());
StringBuffer ss = new StringBuffer().append(r.nextInt(8) + 1);
IntStream ints = r.ints(r.nextInt(200), 0, 9);
ints.forEach(i -> ss.append(i));
System.out.println(ss.toString());
System.out.println(ss.length());
}
/**
* 字符
*/
public static void charHelp() {
System.out.println("\n\ncharHelp:--------------------------\n");
// 判斷字符屬於哪種類型
System.out.println(Character.isWhitespace(' ')); // 空白字符
System.out.println(Character.isDigit('2')); // 數字
System.out.println(Character.isAlphabetic('a')); // 阿拉伯字母
System.out.println(Character.isLowerCase('a')); // 是小寫字母阿拉伯字母
System.out.println(Character.isUpperCase('A')); // 是大寫字母阿拉伯字母
System.out.println(Character.isLetter('士')); // 字符(不包含數字)
System.out.println(Character.isLetterOrDigit('a')); // 字符或數字
// 字符與數字進行轉換
char c = '5';
int cInt = c; // char可以直接轉爲int,但是此時的int是codePoint碼
System.out.println(cInt);
cInt = c - '0'; // char與int運算後轉爲int
System.out.println(cInt);
c = (char) (cInt + '0');
System.out.println(c);
}
/**
* 字符串
*/
public static void stringHelp() {
System.out.println("\n\nstringHelp:--------------------------\n");
String s = "123abc";
// 字符串與字符數組的轉換
char[] cc = s.toCharArray();
System.out.println(cc);
s.getChars(0, 3, cc, 3);
System.out.println(cc);
String cs = new String(cc);
System.out.println(cs);
// 從字符串中獲取某個位置的字符
System.out.println(s.charAt(3));
System.out.println(s.substring(3, 4)); // 不包含後邊界
// 截取字符串
System.out.println(s.substring(0, 3));
System.out.println(s.substring(3));
// 拼接字符串
System.out.println("123" + "abc");
System.out.println(123 + "abc");
System.out.println(1 + 2 + 3 + "abc");
System.out.println("abc" + 1 + 2 + 3);
System.out.println("a" + 1 + 2 + 3);
System.out.println('a' + 1 + 2 + 3);
System.out.println("a" + 'b' + 'c' + 1 + 2 + 3);
System.out.println('a' + 'b' + 'c' + 1 + 2 + 3);
StringBuffer ss = new StringBuffer("abc");
ss.append(1).append(2).append(3);
ss.append('1').append('2').append('3');
ss.append("1").append("2").append("3");
System.out.println(ss);
System.out.println(ss.toString());
// String是不可變的
// StringBuffer是對char[]的封裝,幾乎擁有String的所有方法,並且多了插入和追加
ss.insert(6, 'a').insert(7, 'b').insert(8, 'c');
System.out.println(ss);
ss.insert(6, "abc");
System.out.println(ss);
ss.insert(9, 1).insert(10, 2).insert(11, 3);
System.out.println(ss);
ss.append("abc");
System.out.println(ss);
System.out.println(ss.substring(0, 3));
System.out.println(ss.substring(3));
}
/**
* 數組
*/
public static void arrayHelp() {
System.out.println("\n\narrayHelp:--------------------------\n");
// 數組的定義
char[] chars = new char[6];
char[] chars1 = new char[]{0, 1, 2, 100};
int[] ints = new int[]{0, 1, 2};
char[] chars2 = new char[]{'0', '1', '2'};
char[] chars3 = {'0', '1', '2'};
System.out.println(chars);
System.out.println(chars1);
System.out.println(ints);
System.out.println(chars2);
System.out.println(chars3);
System.out.println(Arrays.toString(chars));
System.out.println(Arrays.toString(chars1));
System.out.println(Arrays.toString(ints));
System.out.println(Arrays.toString(chars2));
System.out.println(Arrays.toString(chars3));
// 填充數組
Arrays.fill(chars, '0');
System.out.println(chars);
Arrays.fill(chars3, '0');
System.out.println(chars3);
Arrays.fill(ints, 2, 3, 0);
System.out.println(Arrays.toString(ints));
// 數組的移動與複製
int[] ints1 = Arrays.copyOf(ints, 6);
System.out.println(Arrays.toString(ints1));
int[] ints2 = Arrays.copyOfRange(ints, 1, 2);
System.out.println(Arrays.toString(ints2));
System.arraycopy(ints1, 0, ints1, 3, 3);
System.out.println(Arrays.toString(ints1));
Arrays.spliterator(ints1);
// 數組equals
int[] a = {1, 2, 3}, b = {1, 2, 3};
System.out.println(Arrays.equals(a, b));
Integer[] a1 = {1, 2, 3}, b1 = {1, 2, 3};
System.out.println(Arrays.deepEquals(a1, b1));
Integer[][] aa1 = {{1, 2, 3}, {4, 5, 6}}, bb1 = {{1, 2, 3}, {4, 5, 6}};
System.out.println(Arrays.equals(aa1, bb1));
System.out.println(Arrays.deepEquals(aa1, bb1));
}
/**
* 集合
*/
public static void collectionHelp() {
// 列表
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.sort(Comparator.naturalOrder()); // 排序
Collections.shuffle(list); // 洗牌,打亂順序
Collections.sort(list); // 排序
list.subList(1, list.size()); // 截斷,前閉後開
// 隊列
Queue<Integer> queue = new LinkedList<>(); // 普通隊列
Deque<Integer> deque = new LinkedList<>(); // 雙端隊列
// 添加、查看和刪除,會拋出異常,不建議使用
queue.add(1);
queue.element();
queue.remove();
// 添加、查看和刪除,不會拋出異常,建議使用
queue.offer(1); // 錄取,提供offer
queue.peek(); // 窺視,偷看
queue.poll(); // 投票,選出,推薦
queue.isEmpty();
queue.size();
// 棧,peek和pop之前要先判斷棧是否爲空
Stack<Integer> stack = new Stack<>();
stack.push(1); // 入棧
stack.peek(); // 查看棧頂元素
stack.pop(); // 出棧
stack.search(1); // 查看某個元素的深度(距離棧頂的距離)
stack.empty(); // 查看棧是否爲空
stack.isEmpty();
stack.size();
// 堆
// PriorityQueue內部採用堆實現,一般會指定容量和比較器,從而實現最大堆或最小堆,操作方法與隊列一致
PriorityQueue heap = new PriorityQueue(10);
}
public static void main(String[] args) {
mathHelp();
numberHelp();
charHelp();
stringHelp();
arrayHelp();
collectionHelp();
}
}
運行結果:
mathHelp:--------------------------
0.7819860238092777
1
2
1
1
1.0
-2.0
2.0
-1.0
1
2
numberHelp:--------------------------
8
5
8294859977328311814892605642973184332510343941111000561159424753892808
70
6121662672570216067385170831808760415863051811266827076840182726241884557615330608
82
charHelp:--------------------------
true
true
true
true
true
true
true
53
5
5
stringHelp:--------------------------
123abc
123123
123123
a
a
123
abc
123abc
123abc
6abc
abc123
a123
103
abc123
300
abc123123123
abc123123123
abc123abc123123
abc123abcabc123123
abc123abc123abc123123
abc123abc123abc123123abc
abc
123abc123abc123123abc
arrayHelp:--------------------------
d
[I@214c265e
012
012
[ , , , , , ]
[ , , , d]
[0, 1, 2]
[0, 1, 2]
[0, 1, 2]
000000
000
[0, 1, 0]
[0, 1, 0, 0, 0, 0]
[1]
[0, 1, 0, 0, 1, 0]
true
true
false
true
Process finished with exit code 0