Java刷題常用的資料結構總結

CrazyPixel發表於2023-02-26

1. 基礎運算

//int型相關操作
Integer.INT_MAX;//int型最大值
Integer.INT_MIN;//int型最小值
long name;//注意:沒有c語言裡面的long long
(int)n1%(int)n2;//取餘運算,針對int型,如果運算數不是int型要注意型別轉換

2. 字串類

//String
String s_name = "";//定義一個空字串
String s=String.valueOf(int);//int型轉字串s
String.length();//獲得字串長度
String.charAt(index);//獲得字串中下標index的字元
string.toCharArray(str);//將字串轉化為字元型陣列

String.substring(start_index,end_index);//獲取字串的子串,範圍是[start_index,end_index);
String1 + String2;//兩個字串進行拼接
string1.concat(string2);//兩個字串進行拼接
/*注意:String 類是不可改變的,所以你一旦建立了 String 物件,那它的值就無法改變了
因此String的“+”拼接效率低下,因為需要建立StringBuilder或者StringBuffer物件來實現
*/
//字串遍歷方法一
for(char c:string.toCharArray()){}
//字串遍歷方法二
for(int i=0;i<string.length();i++){
   string.charAt(i);
   ……
}

//StringBuffer
StringBuffer Name=new StringBuffer();//宣告一個StringBuffer型別的變數
StringBuffer.append(ch);//在尾部新增字元
StringBuffer.toString();//將StringBuffer轉為String型別

//StringBuilder相關操作
StringBuilder Name=new StringBuilder();//定義一個空字串
StringBuilder.append(char/String);//將字元或者字串新增到字串中
StringBuilder.toString();//將StringBuilder型別轉化為String型別
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
LinkedList<E> LLName=new LinkedList<E>();//定義一個連結串列,可根據頭尾操作模擬佇列或者棧
LinkedList<E> listname=new LinkedList<E>(oldlist);//將oldlist的元素複製一份給listname,且是深複製
LinkedList.remove();//從列表中刪除元素,預設刪除第一個元素,類似函式還有removeLast()\removeFirst()
LinkedList.remove(index);//刪除指定下標的元素
LinkedList.add(element);//從列表的末尾新增元素,類似函式還有addLast(element)\addFirst(element)
LinkedList.isEmpty();//判斷連結串列是不是空,是空返回true,不是空返回false
LinkedList.size();//獲得連結串列長度


//ArrayList
ArrayList<E> AL_Name = new ArrayList<E>();//定義一個陣列
ArrayList.add(element);//在陣列尾部新增元素
ArrayList.remove(index);//刪除指定下標的元素
ArrayList.get(index);//獲得指定下標的元素值
ArrayList.size();//獲得陣列大小
ArrayList.indexOf(element);//查詢元素的下標位置

4. 棧和佇列

//棧Stack
Stack<E> stackName=new Stack<E>();//定義一個棧
Stack.push(element);//元素壓入棧中
Stack.pop();//頂部元素彈出棧
Stack.empty();//判斷棧是不是空,如果是空返回true,不是空返回false

//佇列Queue

LinkedList<> queue_name=new LinkedList<>();//佇列結構用LinkedList實現
queue_name.add();//新增元素
queue_name.poll();//彈出元素
queue_name.size();//獲得佇列大小

5. 字典類

//HashMap相關操作
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);//查詢key對應的value並返回
HashMap.containsKey(key);//在map表中是不是存在key,存在的話返回true,不存在返回false

6. 樹

//樹結點定義
public class TreeNode {
  int val;
  TreeNode left;
  TreeNode right;
  TreeNode(int x) { val = x; }
}
//獲取屬性值的兩種方式
   //1、TreeNode A
A.value;
   //2、TreeNode* A
A->value;
//空指標
A==null;//注意是小寫null
//結構體
root.left;
root.right;
root.val;

相關文章