
本文主要介绍了在java刷题的过程中常用的数据结构和常用的内置函数,适合新手入门使用。
1. 基础运算
| |
| Integer.INT_MAX; |
| Integer.INT_MIN; |
| long name; |
| (int)n1%(int)n2; |
2. 字符串类
| |
| String s_name = ""; |
| String s=String.valueOf(int); |
| String.length(); |
| String.charAt(index); |
| string.toCharArray(str); |
| |
| String.substring(start_index,end_index); |
| String1 + String2; |
| string1.concat(string2); |
| |
| |
| |
| |
| for(char c:string.toCharArray()){} |
| |
| for(int i=0;i<string.length();i++){ |
| string.charAt(i); |
| …… |
| } |
| |
| |
| StringBuffer Name=new StringBuffer(); |
| StringBuffer.append(ch); |
| StringBuffer.toString(); |
| |
| |
| StringBuilder Name=new StringBuilder(); |
| StringBuilder.append(char/String); |
| StringBuilder.toString(); |
| StringBuilder.reverse(); |
| |
3. 数组类与链表
| |
| int[] array_Name = new int[length]; |
| int[] array_name=new int[]{初始的元素值}; |
| int N=array.length; |
| array[index]; |
| |
| |
| int[][] name=new int[line_size][row_size]; |
| int line=array.length; |
| int row=array[0].length; |
| |
| |
| Class Node { |
| int val; |
| Node next; |
| public Node(int val){ |
| this.val=val; |
| this.next=null; |
| } |
| } |
| Node node=new Node(value); |
| ListNode.val; |
| ListNode.next; |
| |
| |
| LinkedList<E> LLName=new LinkedList<E>(); |
| LinkedList<E> listname=new LinkedList<E>(oldlist); |
| LinkedList.remove(); |
| LinkedList.remove(index); |
| LinkedList.add(element); |
| LinkedList.isEmpty(); |
| LinkedList.size(); |
| |
| |
| |
| ArrayList<E> AL_Name = new ArrayList<E>(); |
| ArrayList.add(element); |
| ArrayList.remove(index); |
| ArrayList.get(index); |
| ArrayList.size(); |
| ArrayList.indexOf(element); |
4. 栈和队列
| |
| Stack<E> stackName=new Stack<E>(); |
| Stack.push(element); |
| Stack.pop(); |
| Stack.empty(); |
| |
| |
| |
| LinkedList<> queue_name=new LinkedList<>(); |
| queue_name.add(); |
| queue_name.poll(); |
| queue_name.size(); |
| |
5. 字典类
| |
| HashMap<type,type> HM_Name = new HashMap<type,type>(); |
| HashMap<type,type> HM_Name = new HashMap<type,type>(){{put(key,value);put(key,value);}}; |
| HashMap.put(key,value); |
| HashMap.get(key); |
| HashMap.containsKey(key); |
| |
6. 树
| |
| public class TreeNode { |
| int val; |
| TreeNode left; |
| TreeNode right; |
| TreeNode(int x) { val = x; } |
| } |
| |
| |
| A.value; |
| |
| A->value; |
| |
| A==null; |
| |
| root.left; |
| root.right; |
| root.val; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律