
数组是一种线性数据结构,其中所有元素都按顺序排列。它存储在连续内存位置的相同集合数据类型元素。
初始化public class array<t> {
private t[] self;
private int size;
@suppresswarnings("unchecked")
public array(int size) {
if (size
<p>在核心数组类中,我们将存储数组的大小和数组初始化的一般框架。在构造函数中,我们要求数组的大小,创建一个对象,并将其类型转换为我们想要的数组。</p>
<h3>
设置方法
</h3>
<pre class="brush:php;toolbar:false">public void set(t item, int index) {
if (index >= this.size || index
<p>该方法要求将一个项目存储在数组中,并要求存储哪个项目的索引。</p>
<h3>
获取方法
</h3>
<pre class="brush:php;toolbar:false">public t get(int index) {
if (index >= this.size || index
<p>get 该方法要求索引并从索引中检索项目。</p>
<h3>
打印方式
</h3>
<pre class="brush:php;toolbar:false">public void print() {
for (int i = 0; i
<p>print 该方法只是将数组的所有成员打印在一行中,在每个项目之间使用空间分隔。</p>
<h2>
排序数组
</h2>
<p>数组,但具有对元素本身进行排序的功能。</p>
<h3>
初始化
</h3>
<pre class="brush:php;toolbar:false">public class sortedarray<t extends comparable>> {
private t[] array;
private int size;
private final int maxsize;
@suppresswarnings("unchecked")
public sortedarray(int maxsize) {
if (maxsize
<p>在排序数组类中,我们将存储数组的大小,并要求数组的最大大小和数组初始化的一般框架。在构造函数中,我们要求数组的最大尺寸,创建一个对象,并将其类型转换为我们想要的数组。</p>
<h3>
吸气剂
</h3>
<pre class="brush:php;toolbar:false">public int length() {
return this.size;
}
public int maxlength() {
return this.maxsize;
}
public t get(int index) {
if (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 = 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
<h3>
检索方法
</h3>
<pre class="brush:php;toolbar:false">private int binarysearch(t target) {
int left = 0;
int right = size - 1;
while (left
<h3>
遍历法
</h3>
<pre class="brush:php;toolbar:false">public void traverse(callback<t> callback) {
for (int i = 0; i
<h3>
回调接口
</h3>
<pre class="brush:php;toolbar:false">public interface callback<t> {
void call(t item);
}
</t>使用遍历中的回调接口
public class uppercasecallback implements unsortedarray.callback<string> {
@override
public void call(string item) {
system.out.println(item.touppercase());
}
}
</string>未排序的数组
几乎和上面一样 初始化和 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
<h2>
动态数组
</h2>
<p>动态数组就像数组列表或列表。</p>
<h3>
初始化
</h3>
<pre class="brush:php;toolbar:false">public class dynamicarray<t> {
private t[] array;
private int size;
private int capacity;
@suppresswarnings("unchecked")
public dynamicarray(int initialcapacity) {
if (initialcapacity
<h3>
插入方式
</h3>
<pre class="brush:php;toolbar:false">private void resize(int newcapacity) {
@suppresswarnings("unchecked")
t[] newarray = (t[]) new object[newcapacity];
for (int i = 0; i = 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 1 && size
<p>其他的都一样。<br>
希望这对使用数组有帮助。<br>
希望这有助于使用数组。祝你好运!</p>
以上是数据结构:数组的详细内容,请关注图灵教育的其他相关文章!