数组是一种线性数据结构,其中所有元素按顺序排列。它是存储在连续内存位置的相同数据类型元素的集合。
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]; } }
get 方法请求索引并从该索引检索项目。
public void print() { for (int i = 0; i < size; i++) { system.out.println(this.self[i]+" "); } }
print 方法只是将数组的所有成员打印在一行中,每个项目之间用空格分隔。
数组,但具有对元素本身进行排序的功能。
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++; }
insert 方法以排序的形式将项目插入到其位置。
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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号