JAVA核心資料結構與演算法實現
java
資料結構
演算法
由於文章篇幅有限,我將提供一些關鍵資料結構和演算法的實作範例。首先介紹幾個核心資料結構和演算法,然後給出對應的Java程式碼範例。
-
陣列
- 實作一個動態擴充的陣列
- 實作陣列的增刪改查運算
public class DynamicArray<T> { private Object[] array; private int size; private int capacity; public DynamicArray() { capacity = 10; array = new Object[capacity]; size = 0; } public void add(T element) { if (size == capacity) { capacity *= 2; Object[] newArray = new Object[capacity]; System.arraycopy(array, 0, newArray, 0, size); array = newArray; } array[size++] = element; } public T get(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); return (T) array[index]; } public void remove(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); for (int i = index; i < size - 1; i++) { array[i] = array[i + 1]; } size--; } }
登入後複製
鍊錶
- 實作一個單鍊錶
- 實作鍊錶的增刪改查操作
public class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class LinkedList { private ListNode head; public void addAtHead(int val) { ListNode newHead = new ListNode(val); newHead.next = head; head = newHead; } public void addAtTail(int val) { if (head == null) { head = new ListNode(val); } else { ListNode current = head; while (current.next != null) { current = current.next; } current.next = new ListNode(val); } } public void deleteAtIndex(int index) { if (index == 0) { head = head.next; return; } int count = 0; ListNode current = head; ListNode prev = null; while (current != null && count < index) { prev = current; current = current.next; count++; } if (current != null) { prev.next = current.next; } } public ListNode get(int index) { ListNode current = head; int count = 0; while (current != null && count < index) { current = current.next; count++; } return current; } }
登入後複製
堆疊
- 實作一個基於陣列的堆疊
- 實作堆疊的入堆疊、出棧等操作
public class ArrayStack { private int[] array; private int top; private int capacity; public ArrayStack(int capacity) { this.capacity = capacity; array = new int[capacity]; top = -1; } public void push(int value) { if (top == capacity - 1) throw new IllegalStateException("Stack is full"); array[++top] = value; } public int pop() { if (top == -1) throw new IllegalStateException("Stack is empty"); return array[top--]; } public int peek() { if (top == -1) throw new IllegalStateException("Stack is empty"); return array[top]; } public boolean isEmpty() { return top == -1; } }
登入後複製
隊列
- 實作一個基於陣列的佇列
- 實作佇列的入隊、出隊等運算
public class ArrayQueue { private int[] array; private int front; private int rear; private int capacity; public ArrayQueue(int capacity) { this.capacity = capacity; array = new int[capacity]; front = 0; rear = -1; } public void enqueue(int value) { if (rear == capacity - 1) throw new IllegalStateException("Queue is full"); array[++rear] = value; } public int dequeue() { if (isEmpty()) throw new IllegalStateException("Queue is empty"); int value = array[front]; front++; return value; } public int peek() { if (isEmpty()) throw new IllegalStateException("Queue is empty"); return array[front]; } public boolean isEmpty() { return front > rear; } }
登入後複製
排序演算法
- #實作冒泡排序、插入排序、選擇排序、快速排序等演算法
public class Sort { public static void bubbleSort(int[] array) { int length = array.length; for (int i = 0; i < length - 1; i++) { for (int j = 0; j < length - 1 - i; j++) { if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } public static void insertionSort(int[] array) { for (int i = 1; i < array.length; i++) { int current = array[i]; int j = i - 1; while (j >= 0 && array[j] > current) { array[j + 1] = array[j]; j--; } array[j + 1] = current; } } public static void selectionSort(int[] array) { for (int i = 0; i < array.length - 1; i++) { int minIndex = i; for (int j = i + 1; j < array.length; j++) { if (array[j] < array[minIndex]) { minIndex = j; } } int temp = array[i]; array[i] = array[minIndex]; array[minIndex] = temp; } } public static void quickSort(int[] array, int low, int high) { if (low < high) { int mid = partition(array, low, high); quickSort(array, low, mid - 1); quickSort(array, mid + 1, high); } } private static int partition(int[] array, int low, int high) { int pivot = array[high]; int i = low - 1; for (int j = low; j < high; j++) { if (array[j] < pivot) { i++; int temp = array[i]; array[i] = array[j]; array[j] = temp; } } int temp = array[i + 1]; array[i + 1] = array[high]; array[high] = temp; return i + 1; } }
登入後複製
以上是一些核心的資料結構和演算法的Java實作範例,希望能對你有幫助。
以上是JAVA核心資料結構與演算法實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
1 週前
By DDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

Java是熱門程式語言,適合初學者和經驗豐富的開發者學習。本教學從基礎概念出發,逐步深入解說進階主題。安裝Java開發工具包後,可透過建立簡單的「Hello,World!」程式來實踐程式設計。理解程式碼後,使用命令提示字元編譯並執行程序,控制台上將輸出「Hello,World!」。學習Java開啟了程式設計之旅,隨著掌握程度加深,可創建更複雜的應用程式。
