数据结构---数组(java)

这篇具有很好参考价值的文章主要介绍了数据结构---数组(java)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

数组的基本功能:

1 、数组基础
<1> 用来存储一组类型相同的数据
<2> 在内存中,分配连续的空间,数组创建时要指定容量(大小)
<3> 数据类型[] 数组名 int[] arr = new int[10] int[] arr2 = {1,2,3,4}
<4> 索引---访问数组时通过索引进行操作
<5> 索引从0开始,最大为 arr.length -1
<6> 常见的错误: NullPointException ArrayIndexOutOfBoundsException
<7> 常见的数组: 字符串, 对象数组,哈希表
2 、演示数组的使用
3 、使用数组时,最重要的就是数组的 索引 ,通过索引可以对数组进行改和查操作。

1.数组的组成:

这里使用了泛型,数组可以是任何类型
 private int size;//当前元素个数
    private int capacity;//容量
    private T[]data;//数组保存数据

2.数组的构造方法:

 public MyArray(int capacity) {
        this.capacity = capacity;
        this.size=0;
        this.data=(T[])(new Object[this.capacity]);
    }

3.获取数组中元素个数

 //获取数组元素个数
    public int getSize(){
        return this.size;
    }

4.数组的容积:

 //获取数组容积
    public int getCapacity(){
        return this.capacity;
    }

5.判断数组是否为空:

 //判断数组是否为空
    public boolean isEmity(){
        return this.size==0;
    }

6.数组容积的变化:

 private void resize(int newcapacity){
        T[]newdata=(T[])(new Object[newcapacity]);
        for(int i=0;i<this.size;i++){
            newdata[i]=this.data[i];
        }
        this.capacity=newcapacity;
        this.data=newdata;
    }

7.像数组里添加元素:并扩容:

 public void add(T item,int index){
        if(index<0||index>this.size){
            throw new IllegalArgumentException("index is invalid");
        }
        //this.size表示待插入元素的位置
        if(this.size==this.capacity){
            resize(this.capacity*2);
        }
        for(int i=size-1;i>=index;i--){
            this.data[i+1]=this.data[i];
        }
        this.data[index]=item;
        this.size++;

    }

8.修改指定位置的值:

//修改指定位置值
    public void modify(int index,T value){
        if(index<0||index>this.size){
            throw new IllegalArgumentException("index is invalid");
        }
        this.data[index]=value;
    }

9.获取索引位置元素:

  //获取指定索引位置元素
    public T getValue(int index){
        if(index<0||index>this.size){
            throw new IllegalArgumentException("index is invalid");
        }
        return this.data[index];
    }

10.查询指定位置的值在数组中是否存在并返回索引

  //查询指定位置的值在数组中是否存在,返回索引
    public int containsValue(T val){
        for(int i=0;i<this.size;i++){
            if(val.equals(this.data[i])){
                return i;
            }
        }
        return -1;
    }

11.删除索引位置数组元素:以及数组的缩容

缩容是/4来判断,/2来缩容

是为了防止数组的震荡:复杂度的震荡 ,当我们同时进行addLastremoveLast的操作文章来源地址https://www.toymoban.com/news/detail-817777.html

 //根据索引删除数组元素
    public void removeValue(int index){
        if (index < 0 || index > this.size) {
            throw new IllegalArgumentException("index is invalid");
        }
        for (int i = index + 1; i < this.size; i++) {
            this.data[i - 1] = this.data[i];
        }
        if(this.size<this.capacity/4&&this.capacity/2>0){
            resize((this.capacity)/2);
        }

        this.size--;

    }

12.tostring方法重写:

 @Override
    public String toString() {
       StringBuilder sb=new StringBuilder("[");
       for(int i=0;i<this.size;i++){
           sb.append(this.data[i]);
           if(i!=this.size-1){
               sb.append(",");
           }
       }
       sb.append("]");
       return sb.toString();
    }

13.主函数及部分方法应用:

 public static void main(String[] args) {
        MyArray<Integer> myArray=new MyArray<>(3);
        Random random=new Random();
        myArray.add(10,0);
        System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
        for(int i=0;i<3;i++){
            myArray.add(random.nextInt(20),myArray.size);
        }
        myArray.add(40,myArray.size);
        System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
        System.out.println(myArray.toString()+myArray.capacity);
        while(!myArray.isEmity()){
            myArray.removeValue(0);
            System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
        }
    }

完整代码

package com.ffyc.learn;

import java.util.Arrays;
import java.util.Random;

public class MyArray<T> {
    private int size;//当前元素个数
    private int capacity;//容量
    private T[]data;//数组保存数据

    //构造方法
    public MyArray(int capacity) {
        this.capacity = capacity;
        this.size=0;
        this.data=(T[])(new Object[this.capacity]);
    }
    //获取数组元素个数
    public int getSize(){
        return this.size;
    }
    //获取数组容积
    public int getCapacity(){
        return this.capacity;
    }
    //判断数组是否为空
    public boolean isEmity(){
        return this.size==0;
    }
    private void resize(int newcapacity){
        T[]newdata=(T[])(new Object[newcapacity]);
        for(int i=0;i<this.size;i++){
            newdata[i]=this.data[i];
        }
        this.capacity=newcapacity;
        this.data=newdata;
    }
    //数组添加元素
    public void add(T item,int index){
        if(index<0||index>this.size){
            throw new IllegalArgumentException("index is invalid");
        }
        //this.size表示待插入元素的位置
        if(this.size==this.capacity){
            resize(this.capacity*2);
        }
        for(int i=size-1;i>=index;i--){
            this.data[i+1]=this.data[i];
        }
        this.data[index]=item;
        this.size++;

    }
    //修改指定位置值
    public void modify(int index,T value){
        if(index<0||index>this.size){
            throw new IllegalArgumentException("index is invalid");
        }
        this.data[index]=value;
    }
    //获取指定索引位置元素
    public T getValue(int index){
        if(index<0||index>this.size){
            throw new IllegalArgumentException("index is invalid");
        }
        return this.data[index];
    }
    //查询指定位置的值在数组中是否存在,返回索引
    public int containsValue(T val){
        for(int i=0;i<this.size;i++){
            if(val.equals(this.data[i])){
                return i;
            }
        }
        return -1;
    }
    //根据索引删除数组元素
    public void removeValue(int index){
        if (index < 0 || index > this.size) {
            throw new IllegalArgumentException("index is invalid");
        }
        for (int i = index + 1; i < this.size; i++) {
            this.data[i - 1] = this.data[i];
        }
        if(this.size<this.capacity/4&&this.capacity/2>0){
            resize((this.capacity)/2);
        }

        this.size--;

    }

    @Override
    public String toString() {
       StringBuilder sb=new StringBuilder("[");
       for(int i=0;i<this.size;i++){
           sb.append(this.data[i]);
           if(i!=this.size-1){
               sb.append(",");
           }
       }
       sb.append("]");
       return sb.toString();
    }

    public static void main(String[] args) {
        MyArray<Integer> myArray=new MyArray<>(3);
        Random random=new Random();
        myArray.add(10,0);
        System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
        for(int i=0;i<3;i++){
            myArray.add(random.nextInt(20),myArray.size);
        }
        myArray.add(40,myArray.size);
        System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
        System.out.println(myArray.toString()+myArray.capacity);
        while(!myArray.isEmity()){
            myArray.removeValue(0);
            System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
        }
    }
}

到了这里,关于数据结构---数组(java)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 数据结构与算法 | 数组(Array)

    数组(Array)应该是最基础的数据结构之一,它由相同类型的元素组成的集合,并按照一定的顺序存储在内存中。每个元素都有一个唯一的索引,可以用于访问该元素。 数组索引(Index): 数组中的每个元素都有一个唯一的整数索引,从0开始计数。索引用于访问数组中的元素

    2024年02月08日
    浏览(14)
  • JavaScript数据结构与算法整理------数组

            数组的标准定义: 一个存储元素的线性集合,元素可以通过索引来任意存取,索引通常是数字,用来计算元素之间存储位置的偏移量 ,几乎所有的编程语言都有类似的数据结构,而JavaScript的数组略有不同。         JavaScript中的数组是一种特殊的对象,用来表示偏

    2023年04月24日
    浏览(23)
  • 数据结构与算法-数组(附阿里面试题)

            给你一个文件里面包含全国人民(14亿)的年龄数据(0~180),现在要你统计每一个年龄   有多少人?          给定机器为 单台+2CPU+2G内存。不得使用现成的容器,比如map等。 (这一句可以忽略)         在以上情况下你该如何以最高效的方法来解决这个

    2024年02月13日
    浏览(13)
  • 【数据结构和算法】寻找数组的中心下标

    Java基础合集 数据结构与算法合集 设计模式合集 多线程合集 分布式合集 ES合集 其他系列文章导航 文章目录 前言 一、题目描述 二、题解 2.1 前缀和的解题模板 2.1.1 最长递增子序列长度 2.1.2 寻找数组中第 k 大的元素 2.1.3 最长公共子序列长度 2.1.4 寻找数组中第 k 小的元素 2

    2024年02月04日
    浏览(18)
  • 【数据结构和算法】使用数组的结构实现链表(单向或双向)

    上文我们通过结构体的结构实现了队列 、以及循环队列的实现,我们或许在其他老师的教学中,只学到了用结构体的形式来实现链表、队列、栈等数据结构,本文我想告诉你的是,我们 可以使用数组的结构实现链表、单调栈、单调队列 目录 前言 一、用数组结构的好处 1.数

    2024年01月20日
    浏览(28)
  • 数据结构---数组(java)

    1 、数组基础 1 用来存储一组类型相同的数据 2 在内存中,分配连续的空间,数组创建时要指定容量(大小) 3 数据类型[] 数组名 int[] arr = new int[10] int[] arr2 = {1,2,3,4} 4 索引---访问数组时通过索引进行操作 5 索引从0开始,最大为 arr.length -1 6 常见的错误: NullPointException ArrayI

    2024年01月23日
    浏览(18)
  • 【数据结构与算法——TypeScript】数组、栈、队列、链表

    解决问题 的过程中,不仅仅 数据的存储方式会影响效率,算法的优劣也会影响效率 什么是算法? 定义: 🟢 一个有限指令集,每条指令的描述不依赖于言语 (编写指令:java/c++/ts/js) 🟢 接收一些输入(有些情况下不需要输入)(接收:排序:无序数组) 🟢 产生输出 (

    2024年02月14日
    浏览(13)
  • 数据结构与算法·第5章【数组和广义表】

    两种顺序映象的方式: 以行序为主序(低下标优先); 以列序为主序(高下标优先)。 而 n n n 维数组: LOC(x1, x2, ..., xn) = LOC(0, 0, ..., 0) + [(x1 × b1 + x2) × b2 + x3] × b3 + ... + xn 数据类型定义 其中: A.bounds是每一维可以放多少元素: a[A.bounds[0]][A.bounds[1]][A.bounds[2]]…… A.constants是指向每

    2024年02月08日
    浏览(19)
  • 算法与数据结构(二十四)最优子结构原理和 dp 数组遍历方向

    注:此文只在个人总结 labuladong 动态规划框架,仅限于学习交流,版权归原作者所有; 本文是两年前发的 动态规划答疑篇open in new window 的修订版,根据我的不断学习总结以及读者的评论反馈,我给扩展了更多内容,力求使本文成为继 动态规划核心套路框架 之后的一篇全面

    2024年02月12日
    浏览(10)
  • 数据结构Java版(1)——数组

    是一门基础学科 研究的是数据如何在计算机中进行组织和存储,使得我们可以高效的获取数据和修改数据 数据结构可以分为三类: 线性结构: 数组、队列、栈、链表、哈希表… 树型结构:二叉树、二分搜索树、AVL树,红黑树、堆、Trie、线段树、并查集… 图结构:邻接矩阵

    2024年01月19日
    浏览(19)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包