静态数组
数组是一种线性数据结构,其中所有元素按顺序排列。它是存储在连续内存位置的相同数据类型元素的集合。
初始化
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>获取方法请求索引并从该索引检索项目。</p> <h3> 打印方式 </h3> <pre class="brush:php;toolbar:false">public void print() { for (int i = 0; i <p>打印方法只是将数组的所有成员打印在一行中,每个项目之间用空格分隔。</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++; }
插入方法以排序的形式将项目插入到其位置。
删除方法
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> 希望这有助于使用数组。祝你好运!</p>
以上是数据结构:数组的详细内容。更多信息请关注PHP中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA

本文解释了用于构建分布式应用程序的Java的远程方法调用(RMI)。 它详细介绍了接口定义,实现,注册表设置和客户端调用,以解决网络问题和安全性等挑战。

本文详细介绍了用于网络通信的Java的套接字API,涵盖了客户服务器设置,数据处理和关键考虑因素,例如资源管理,错误处理和安全性。 它还探索了性能优化技术,我

本文详细介绍了创建自定义Java网络协议。 它涵盖协议定义(数据结构,框架,错误处理,版本控制),实现(使用插座),数据序列化和最佳实践(效率,安全性,维护


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

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

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

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

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中