数组是一种线性数据结构,其中所有元素按顺序排列。它是存储在连续内存位置的相同数据类型元素的集合。
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中文网其他相关文章!