0

0

数据结构:数组

PHPz

PHPz

发布时间:2024-08-11 21:12:03

|

460人浏览过

|

来源于dev.to

转载

数据结构:数组

静态数组

数组是一种线性数据结构,其中所有元素按顺序排列。它是存储在连续内存位置的相同数据类型元素的集合。

初始化

public class array {
    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 方法只是将数组的所有成员打印在一行中,每个项目之间用空格分隔。

排序数组

数组,但具有对元素本身进行排序的功能。

viable
viable

基于GPT-4的AI非结构化数据分析平台

下载

初始化

public class sortedarray> {
    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 callback) {
        for (int i = 0; i < this.size; i++) {
            callback.call(this.array[i]);
        }
    }

回调接口

public interface callback {
        void call(t item);
    }

遍历中回调接口的使用

public class uppercasecallback implements unsortedarray.callback {
    @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 {
    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);
        }
    }

其他都一样。
希望这有助于使用数组。祝你好运!

相关专题

更多
python中print函数的用法
python中print函数的用法

python中print函数的语法是“print(value1, value2, ..., sep=' ', end=' ', file=sys.stdout, flush=False)”。本专题为大家提供print相关的文章、下载、课程内容,供大家免费下载体验。

183

2023.09.27

数据类型有哪几种
数据类型有哪几种

数据类型有整型、浮点型、字符型、字符串型、布尔型、数组、结构体和枚举等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

297

2023.10.31

php数据类型
php数据类型

本专题整合了php数据类型相关内容,阅读专题下面的文章了解更多详细内容。

216

2025.10.31

treenode的用法
treenode的用法

​在计算机编程领域,TreeNode是一种常见的数据结构,通常用于构建树形结构。在不同的编程语言中,TreeNode可能有不同的实现方式和用法,通常用于表示树的节点信息。更多关于treenode相关问题详情请看本专题下面的文章。php中文网欢迎大家前来学习。

529

2023.12.01

C++ 高效算法与数据结构
C++ 高效算法与数据结构

本专题讲解 C++ 中常用算法与数据结构的实现与优化,涵盖排序算法(快速排序、归并排序)、查找算法、图算法、动态规划、贪心算法等,并结合实际案例分析如何选择最优算法来提高程序效率。通过深入理解数据结构(链表、树、堆、哈希表等),帮助开发者提升 在复杂应用中的算法设计与性能优化能力。

5

2025.12.22

硬盘接口类型介绍
硬盘接口类型介绍

硬盘接口类型有IDE、SATA、SCSI、Fibre Channel、USB、eSATA、mSATA、PCIe等等。详细介绍:1、IDE接口是一种并行接口,主要用于连接硬盘和光驱等设备,它主要有两种类型:ATA和ATAPI,IDE接口已经逐渐被SATA接口;2、SATA接口是一种串行接口,相较于IDE接口,它具有更高的传输速度、更低的功耗和更小的体积;3、SCSI接口等等。

989

2023.10.19

PHP接口编写教程
PHP接口编写教程

本专题整合了PHP接口编写教程,阅读专题下面的文章了解更多详细内容。

50

2025.10.17

php8.4实现接口限流的教程
php8.4实现接口限流的教程

PHP8.4本身不内置限流功能,需借助Redis(令牌桶)或Swoole(漏桶)实现;文件锁因I/O瓶颈、无跨机共享、秒级精度等缺陷不适用高并发场景。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

195

2025.12.29

vlookup函数使用大全
vlookup函数使用大全

本专题整合了vlookup函数相关 教程,阅读专题下面的文章了解更多详细内容。

28

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 10万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 2.7万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号