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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++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中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

Spring Boot简化了可靠,可扩展和生产就绪的Java应用的创建,从而彻底改变了Java开发。 它的“惯例惯例”方法(春季生态系统固有的惯例),最小化手动设置

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。
