输入输出
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String str = scanner.nextLine();
System.out.println(str);
}
数据结构
栈
Stack stack = new Stack();
stack.push(1);
stack.push(2);
System.out.println(stack.empty());
System.out.println(stack.size());
System.out.println(stack.peek());
System.out.println(stack.pop());
System.out.println(stack.size());
}
普通队列
Queue queue = new LinkedList();
queue.offer(1);
queue.offer(2);
queue.offer(3);
System.out.println(queue.isEmpty());
System.out.println(queue.peek());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
双端队列
Deque deque = new LinkedList<>();
deque.isEmpty();
deque.peekFirst();
deque.peekLast();
deque.addFirst(2);
deque.addLast(1);
deque.removeLast();
deque.removeFirst();
数组和List转换
List<Integer> lists = Arrays.asList(1, 2, 3, 4);
int[] num = {1, 2, 3, 4};
List<Integer> collect = Arrays.stream(num).boxed().collect(Collectors.toList());
Collections.sort(collect);
System.out.println("collect排序后:" + collect);
Collections.reverse(collect);
System.out.println("collect反转后:" + collect);
String[] strArr = new String[] {"1", "2", "3", "4"};
List<String> strings = Arrays.asList(strArr);
System.out.println(strings.subList(1, 2));
List<Integer> list2 = new ArrayList<>();
list2.add(1);
list2.add(2);
list2.add(3);
Object[] objects = list2.toArray();
System.out.println(Arrays.toString(objects));
Integer[] intArr = new Integer[list2.size()];
Integer[] integers = list2.toArray(intArr);
System.out.println(Arrays.toString(integers));
List<String> list4 = Arrays.asList("1", "3", "5");
int[] ints = list4.stream().mapToInt(Integer::new).toArray();
int[] num3 = {1, 2, 4, 1, 5, 6, 23};
Arrays.sort(num3);
System.out.println(Arrays.toString(num3));
字符串
String str = "abcdefg-hijk";
for (int i = 0, len = str.length(); i < len; i++) {
char charAt = str.charAt(i);
System.out.print(charAt);
}
System.out.println("============");
char[] chars = str.toCharArray();
System.out.println(Arrays.toString(chars));
String str2 = "0123456";
System.out.println(str2.substring(5));
System.out.println(str2.substring(5, 6));
System.out.println(str2.concat("a"));
System.out.println(str2.contains("a"));
String str3 = str2.concat("a");
System.out.println(str3.contains("a"));
System.out.println("abc".compareTo("aab"));
String str4 = "abc";
System.out.println(str4.matches("[a-z]*"));
StringBuilder builder = new StringBuilder();
StringBuilder append = builder.append(str4);
System.out.println(append.reverse());
进制转换
String str="5B";
Integer integer10 = Integer.valueOf(str, 16);
System.out.println("10进制="+integer10);
System.out.println("2进制="+Integer.toBinaryString(integer10));
System.out.println("8进制="+Integer.toOctalString(integer10));
System.out.println("16进制="+Integer.toHexString(integer10));
Math数学计算
float numf=1.35F;
double numd=3.14;
System.out.println("向上取整="+(int)Math.ceil(numf));
System.out.println("向下取整="+(int)Math.floor(numf));
System.out.println("四舍五入="+(int)Math.round(numf));
System.out.println("次方="+(int)Math.pow(2,3));
character
char charStr = 'a';
System.out.println("是字母:" + Character.isLetter(charStr));
System.out.println("是数字:" + Character.isDigit(charStr));
System.out.println("是空格:" + Character.isWhitespace(charStr));
System.out.println("是大写:" + Character.isUpperCase(charStr));
System.out.println("是小写:" + Character.isLowerCase(charStr));
System.out.println("转大写:" + Character.toUpperCase(charStr));
System.out.println("转string:" + Character.toString(charStr));