当前位置: 首页 > news >正文

Java算法竞赛常用API指南

Java算法竞赛常用API指南

1. 排序相关API

基本类型数组排序

import java.util.Arrays;int[] arr = {5, 3, 9, 1, 6};
Arrays.sort(arr); // 默认升序排序
// arr = [1, 3, 5, 6, 9]

对象数组排序

Integer[] arr = {5, 3, 9, 1, 6};
Arrays.sort(arr); // 升序
Arrays.sort(arr, Collections.reverseOrder()); // 降序

自定义排序

// 1. 自定义Comparator
Integer[] arr = {5, 3, 9, 1, 6};
Arrays.sort(arr, (a, b) -> b - a); // 降序// 2. 对自定义对象排序
class Person {
String name;
int age;
// constructor etc.
}Person[] people = ...;
Arrays.sort(people, (p1, p2) -> {
if (p1.age != p2.age) return p1.age - p2.age;
return p1.name.compareTo(p2.name);
});

部分排序

int[] arr = {5, 3, 9, 1, 6, 2, 8};
Arrays.sort(arr, 1, 5); // 只排序索引1到4的元素 [5, 1, 3, 6, 9, 2, 8]

2. 集合类API

ArrayList (动态数组)

import java.util.ArrayList;ArrayList<Integer> list = new ArrayList<>();
list.add(5);// 添加元素
list.add(1, 10);// 在索引1处插入10
list.get(0);// 获取索引0的元素
list.set(0, 7);// 设置索引0的元素为7
list.remove(0);// 移除索引0的元素
list.size();// 获取大小
list.contains(5);// 检查是否包含
list.indexOf(5);// 查找元素索引
Collections.sort(list); // 排序

HashSet (无序集合)

import java.util.HashSet;HashSet<Integer> set = new HashSet<>();
set.add(5);// 添加元素
set.contains(5);// 检查存在
set.remove(5);// 移除元素
set.size();// 获取大小
// 遍历
for (int num : set) {
System.out.println(num);
}

TreeSet (有序集合)

import java.util.TreeSet;TreeSet<Integer> set = new TreeSet<>();
set.add(5);
set.add(3);
set.add(9);set.first();// 最小元素 (3)
set.last();// 最大元素 (9)
set.lower(5);// 小于5的最大元素 (3)
set.higher(5);// 大于5的最小元素 (9)
set.floor(5);// 小于等于5的最大元素 (5)
set.ceiling(5);// 大于等于5的最小元素 (5)

3. 映射类API

HashMap (哈希表)

import java.util.HashMap;HashMap<String, Integer> map = new HashMap<>();
map.put("Alice", 25);// 添加键值对
map.get("Alice");// 获取值 (25)
map.containsKey("Alice"); // 检查键是否存在
map.getOrDefault("Bob", 0); // 如果不存在返回默认值0
map.remove("Alice");// 移除键值对
map.size();// 获取大小// 遍历
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}

TreeMap (有序映射)

import java.util.TreeMap;TreeMap<Integer, String> map = new TreeMap<>();
map.put(3, "Three");
map.put(1, "One");
map.put(2, "Two");map.firstKey();// 最小键 (1)
map.lastKey();// 最大键 (3)
map.lowerKey(2);// 小于2的最大键 (1)
map.higherKey(2);// 大于2的最小键 (3)
map.floorKey(2);// 小于等于2的最大键 (2)
map.ceilingKey(2);// 大于等于2的最小键 (2)

4. 队列和栈

Queue (队列)

import java.util.Queue;
import java.util.LinkedList;Queue<Integer> queue = new LinkedList<>();
queue.offer(5);// 入队 (推荐使用offer而不是add)
queue.poll();// 出队 (推荐使用poll而不是remove)
queue.peek();// 查看队首元素
queue.isEmpty();// 检查是否为空
queue.size();// 获取大小

PriorityQueue (优先队列/堆)

import java.util.PriorityQueue;// 最小堆 (默认)
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
minHeap.offer(5);
minHeap.offer(1);
minHeap.peek(); // 1 (堆顶最小元素)// 最大堆
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
maxHeap.offer(5);
maxHeap.offer(1);
maxHeap.peek(); // 5 (堆顶最大元素)// 自定义比较器
PriorityQueue<Person> pq = new PriorityQueue<>((p1, p2) -> p1.age - p2.age);

Stack (栈)

import java.util.Stack;Stack<Integer> stack = new Stack<>();
stack.push(5);// 压栈
stack.pop();// 弹栈
stack.peek();// 查看栈顶
stack.isEmpty();// 检查是否为空
stack.size();// 获取大小

5. 其他实用API

字符串处理

String s = "Hello,World";
s.charAt(0);// 'H'
s.substring(0, 5);// "Hello"
s.split(",");// ["Hello", "World"]
s.indexOf("World");// 6
s.toLowerCase();// 转小写
s.toUpperCase();// 转大写
s.replace("H", "J");// 替换
String.join("-", "A", "B", "C"); // "A-B-C"

数学工具

import java.lang.Math;Math.max(5, 3);// 5
Math.min(5, 3);// 3
Math.abs(-5);// 5
Math.pow(2, 3);// 8.0
Math.sqrt(16);// 4.0
Math.ceil(3.2);// 4.0
Math.floor(3.8);// 3.0
Math.round(3.5);// 4
Math.random();// [0,1)随机数

大数类 (BigInteger/BigDecimal)

import java.math.BigInteger;BigInteger a = new BigInteger("12345678901234567890");
BigInteger b = new BigInteger("98765432109876543210");
a.add(b);// 加法
a.subtract(b);// 减法
a.multiply(b);// 乘法
a.divide(b);// 除法
a.mod(b);// 取模
a.pow(10);// 幂运算
a.compareTo(b); // 比较

6. 其他

  1. 使用StringBuilder拼接字符串
StringBuilder sb = new StringBuilder();
sb.append("Hello").append(" ").append("World");
String result = sb.toString();
  1. 数组与集合转换
List<Integer> list = Arrays.asList(1, 2, 3);
Integer[] arr = list.toArray(new Integer);// Java 8+ 流式操作
int[] primitiveArr = list.stream().mapToInt(i -> i).toArray();
  1. 自定义比较器的简化写法
Arrays.sort(points, Comparator.comparingInt(p -> p.x)); // 按x坐标排序
Arrays.sort(points, Comparator.comparingInt(p -> p.y).reversed()); // 按y坐标降序
  1. 字符串与字符数组转换

String 转 char[] (Java)

String str = "Hello";
char[] charArray = str.toCharArray(); // 将字符串转换为字符数组// 示例使用
for (char c : charArray) {System.out.println(c); // 逐个输出字符: H, e, l, l, o
}

char[] 转 String (Java)

char[] charArray = {'W', 'o', 'r', 'l', 'd'};
String str = new String(charArray); // 将字符数组转换为字符串
String str2 = String.valueOf(charArray); // 另一种转换方式
  1. 进制转换

十进制转二进制字符串

int num = 10;
String binaryString = Integer.toBinaryString(num); // "1010"

文章转载自:

http://Ok6FufyZ.pqjLp.cn
http://BjVF3cpQ.pqjLp.cn
http://MGqdLnup.pqjLp.cn
http://BPPDjKjw.pqjLp.cn
http://MkYYdPMX.pqjLp.cn
http://bVHDHH4C.pqjLp.cn
http://scuHeAIw.pqjLp.cn
http://HodNiBkF.pqjLp.cn
http://hHVz8aQa.pqjLp.cn
http://aXXiW3Fq.pqjLp.cn
http://rOWRIoMr.pqjLp.cn
http://syISSlmo.pqjLp.cn
http://PmhonW3v.pqjLp.cn
http://bwaUawFF.pqjLp.cn
http://wW7ZwiBV.pqjLp.cn
http://Aa2Lr5Nd.pqjLp.cn
http://64hg5Pu3.pqjLp.cn
http://7GdR8ii0.pqjLp.cn
http://RxpguONu.pqjLp.cn
http://xt7B5xUY.pqjLp.cn
http://03w1xIit.pqjLp.cn
http://pxhwJNje.pqjLp.cn
http://kwxmX6Qo.pqjLp.cn
http://WXvj1lem.pqjLp.cn
http://VUZVHsmt.pqjLp.cn
http://9rqeAxZ8.pqjLp.cn
http://WC1255Uo.pqjLp.cn
http://AQhQyWFi.pqjLp.cn
http://N5hBfZME.pqjLp.cn
http://mUEQMZ2A.pqjLp.cn
http://www.dtcms.com/a/384056.html

相关文章:

  • Hive与Pig核心知识点总结:Hadoop生态下的数据处理工具
  • Vite 项目使用 Vercel 自动化部署完整流程
  • 1. 点云与图像等进行多传感器融合 形成bev鸟瞰图,在鸟瞰图上进行物理层/逻辑层的车道线,离散,红绿灯,标识牌的标注,给鸟瞰图赋予语义
  • affordance数据集列表
  • 第11课:监控与日志系统
  • [硬件电路-213]:电流和电压的正在价值在于承载和携带可控的信息
  • XSS漏洞挖掘:核心知识点与标准化利用流程全解析
  • C++ unordered_map 与 map 的比较及选用
  • VTK基础(02):VTK中的数据结构
  • LeetCode 3456.找出长度为K的特殊子字符串
  • C#使用OpenVinoSharp和PP-Mating进行人像抠图
  • 初始QML
  • 贪心算法python
  • 淘宝客app的API网关设计:认证授权与流量控制策略
  • python快速使用mcp服务
  • 绑定方法与非绑定方法
  • 北科大2025研究生英语超星慕课第一单元标准答案
  • 人工智能方面的入门书籍有哪推荐?
  • STL之string类(C++)
  • 大模型原理的解析
  • Java 事务失效场景全解析
  • 简陋的进度条程序
  • SpringAOP中的通知类型
  • Python之文件读写 day9
  • 深度学习和神经网络之间有什么区别?
  • Linux驱动学习(SPI驱动)
  • 【MySQL|第七篇】DDL语句——数据库定义语言
  • 计算机毕设选题推荐:基于Java+SpringBoot物品租赁管理系统【源码+文档+调试】
  • Redis集群部署模式全解析:原理、优缺点与场景适配
  • ESP32的烧录和执行流程