数据结构:数组
静态数组
数组是一种线性数据结构,其中所有元素按顺序排列。它是存储在连续内存位置的相同数据类型元素的集合。
初始化
public class Array<T> { private T[] self; private int size; @SuppressWarnings("unchecked") public Array(int size) { if (size <= 0) { throw new IllegalArgumentException("Invalid array size (must be positive): " + size); } else { this.size = size; this.self = (T[]) new Object[size]; } } }
在核心数组类中,我们将存储数组的大小和数组初始化的一般框架。在构造函数中,我们要求数组的大小并创建一个对象并将其类型转换为我们想要的数组。
设置方法
public void set(T item, int index) { if (index >= this.size || index < 0) { throw new IndexOutOfBoundsException("Index Out of bounds: " + index); } else { this.self[index] = item; } }
此方法要求将一个项目存储在数组中,并要求存储该项目的索引。
获取方法
public T get(int index) { if (index >= this.size || index < 0) { throw new IndexOutOfBoundsException("Index Out of bounds"); } else { return self[index]; } }
获取方法请求索引并从该索引检索项目。
打印方式
public void print() { for (int i = 0; i < size; i++) { System.out.println(this.self[i]+" "); } }
打印方法只是将数组的所有成员打印在一行中,每个项目之间用空格分隔。
排序数组
数组,但具有对元素本身进行排序的功能。
初始化
public class SortedArray<T extends Comparable<T>> { private T[] array; private int size; private final int maxSize; @SuppressWarnings("unchecked") public SortedArray(int maxSize) { if (maxSize <= 0) { throw new IllegalArgumentException("Invalid array max size (must be positive): " + maxSize); } this.array = (T[]) new Comparable[maxSize]; this.size = 0; this.maxSize = maxSize; } }
在排序数组类中,我们将存储数组的大小并询问数组的最大大小以及数组初始化的一般框架。在构造函数中,我们要求数组的最大大小并创建一个对象并将其类型转换到我们想要的数组中。
吸气剂
public int length() { return this.size; } public int maxLength() { return this.maxSize; } public T get(int index) { if (index < 0 || index >= this.size) { throw new IndexOutOfBoundsException("Index out of bounds: " + index); } return this.array[index]; }
插入方式
private int findInsertionPosition(T item) { int left = 0; int right = size - 1; while (left <= right) { int mid = (left + right) / 2; int cmp = item.compareTo(this.array[mid]); if (cmp < 0) { right = mid - 1; } else { left = mid + 1; } } return left; } public void insert(T item) { if (this.size >= this.maxSize) { throw new IllegalStateException("The array is already full"); } int position = findInsertionPosition(item); for (int i = size; i > position; i--) { this.array[i] = this.array[i - 1]; } this.array[position] = item; size++; }
插入方法以排序的形式将项目插入到其位置。
删除方法
public void delete(T item) { int index = binarySearch(item); if (index == -1) { throw new IllegalArgumentException("Unable to delete element " + item + ": the entry is not in the array"); } for (int i = index; i < size - 1; i++) { this.array[i] = this.array[i + 1]; } this.array[size - 1] = null; size--; }
检索方法
private int binarySearch(T target) { int left = 0; int right = size - 1; while (left <= right) { int mid = (left + right) / 2; int cmp = target.compareTo(this.array[mid]); if (cmp == 0) { return mid; } else if (cmp < 0) { right = mid - 1; } else { left = mid + 1; } } return -1; } public Integer find(T target) { int index = binarySearch(target); return index == -1 ? null : index; }
遍历法
public void traverse(Callback<T> callback) { for (int i = 0; i < this.size; i++) { callback.call(this.array[i]); } }
回调接口
public interface Callback<T> { void call(T item); }
遍历中回调接口的使用
public class UppercaseCallback implements UnsortedArray.Callback<String> { @Override public void call(String item) { System.out.println(item.toUpperCase()); } }
未排序的数组
和上面几乎一样
初始化和 getter 是相同的。
插入方式
public void insert(T item) { if (this.size >= this.maxSize) { throw new IllegalStateException("The array is already full"); } else { this.self[this.size] = item; this.size++; } }
删除方法也是一样
检索方式
public Integer find(T target) { for (int i = 0; i < this.size; i++) { if (this.self[i].equals(target)) { return i; } } return null; }
动态数组
动态数组就像数组列表或列表。
初始化
public class DynamicArray<T> { private T[] array; private int size; private int capacity; @SuppressWarnings("unchecked") public DynamicArray(int initialCapacity) { if (initialCapacity <= 0) { throw new IllegalArgumentException("Invalid initial capacity: " + initialCapacity); } this.capacity = initialCapacity; this.array = (T[]) new Object[initialCapacity]; this.size = 0; } }
插入方式
private void resize(int newCapacity) { @SuppressWarnings("unchecked") T[] newArray = (T[]) new Object[newCapacity]; for (int i = 0; i < size; i++) { newArray[i] = array[i]; } array = newArray; capacity = newCapacity; } public void insert(T item) { if (size >= capacity) { resize(2 * capacity); } array[size++] = item; }
删除方法
public void delete(T item) { int index = find(item); if (index == -1) { throw new IllegalArgumentException("Item not found: " + item); } for (int i = index; i < size - 1; i++) { array[i] = array[i + 1]; } array[--size] = null; if (capacity > 1 && size <= capacity / 4) { resize(capacity / 2); } }
其他一切都一样。
希望这有助于使用数组。祝你好运!
以上是数据结构:数组的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

在使用IntelliJIDEAUltimate版本启动Spring...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

在使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名以构建查询条件,是一个常见的难题。本文将针...
