自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

數(shù)組探秘:數(shù)據(jù)結(jié)構(gòu)的基礎(chǔ),深入解讀數(shù)組的內(nèi)部機制!

大數(shù)據(jù) 數(shù)據(jù)分析
數(shù)組當(dāng)中的元素在內(nèi)存中是連續(xù)存儲的,且每個元素占用相同的內(nèi)存、數(shù)組是有序的、數(shù)組的長度一旦確定是不可變的、當(dāng)然數(shù)組也可以通過System.arraycopy方法進行數(shù)組的替換,簡單理解就是擴容。

 概述 

數(shù)組類似一種集合或列表,比如購物清單,我們看到的返回的一條條數(shù)據(jù),都稱之為數(shù)組,數(shù)組在內(nèi)存中是一塊連續(xù)的空間,可以保存相同類型的多個數(shù)據(jù)的容器、當(dāng)然其中包括一維數(shù)組,二維數(shù)組,n維數(shù)組,但是一般常用的也就前面兩個。

 特征

數(shù)組包含列表的特征,同時具有自己獨有的特征,數(shù)組中存在一個重要的概念稱之為索引。

首先數(shù)組會有數(shù)字構(gòu)成的索引來表示元素所在的位置,索引的位置是從0開始的,我們可以根據(jù)索引來快速訪問數(shù)組的元素。

圖片圖片

數(shù)組當(dāng)中的元素在內(nèi)存中是連續(xù)存儲的,且每個元素占用相同的內(nèi)存、數(shù)組是有序的、數(shù)組的長度一旦確定是不可變的、當(dāng)然數(shù)組也可以通過System.arraycopy方法進行數(shù)組的替換,簡單理解就是擴容。

 模擬數(shù)組的基本操作

其中包括添加,修改,刪除,遍歷,擴容等操作

package com.structure.array;


/**
 * Created by IDEA
 *
 * @author:m1397
 * @Time:2022/4/4 - 7:11
 * 數(shù)組:是有限個類型相同的變量所組成的有序集合,元素與元素之間緊密排列,既不能打亂元素的存儲順序,也不能跳過某個單元進行存儲
 */


public class ArrayDemo {


    /**
     * 數(shù)據(jù)大小
     */
    private int size;
    /**
     * 數(shù)組對象
     */
    private int[] data;
    /**
     * 當(dāng)前存儲數(shù)量
     */
    private int num;


    private ArrayDemo(int size) {
        this.size = size;
        this.data = new int[size];
        this.num = 0;
    }




    public static void main(String[] args) {
        /**
         * 數(shù)組的基本操作
         */
        ArrayDemo arrayDemo = new ArrayDemo(5);
        arrayDemo.insert(1, 0);
        arrayDemo.insert(3, 1);
        arrayDemo.insert(4, 2);
        arrayDemo.insert(2, 3);
        arrayDemo.insert(3, 4);
        System.out.println("11 === " + System.currentTimeMillis());
        arrayDemo.insert(6, 3);
        System.out.println("22 === " + System.currentTimeMillis());
        arrayDemo.print();


    }


    /**
     * 讀取元素
     */
    public void print() {
        System.out.println("index = " + num);
        System.out.println("size = " + this.size);
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }


    public void print(int index) {
        System.out.println(this.data[index]);
    }


    /**
     * 插入元素,含擴容
     *
     * @param element 元素
     * @param index   插入位置
     */
    public void insert(int element, int index) {
        if (index < 0 || index > this.size) {
            throw new RuntimeException("超出數(shù)組實際的元素范圍!!!");
        }
        if (this.num >= this.size) {
            resize();
        }
        // 遍歷數(shù)組,將需要插入的位置之后的元素都往后移動一位
        // 首先得移動存在數(shù)據(jù)的那個位置
        // index為下標(biāo)
        for (int i = this.num - 1; i > index - 1; i--) {
            data[i + 1] = data[i];
        }
        data[index] = element;
        this.num++;
    }


    /**
     * 擴容
     * 將size擴容2倍,復(fù)制到新數(shù)組里面
     */
    private void resize() {
        int length = this.size;
        this.size = this.size * 2;
        int[] arrayNew = new int[this.size];
        System.arraycopy(data, 0, arrayNew, 0, length);
        data = arrayNew;
    }


    /**
     * 刪除元素
     *
     * @param index 刪除指定位置的元素
     * @return 返回刪除的元素
     */
    public int delete(int index) {
        if (index < 0 || index > this.size) {
            throw new RuntimeException("超出數(shù)組實際的元素范圍!!!");
        }
        int deleteElement = data[index];
        // 遍歷數(shù)組,將需要刪除的位置之后的元素都往前移動一位
        for (int i = index; i < this.num; i++) {
            data[i] = data[i + 1];
        }
        this.num--;
        return deleteElement;
    }


    /**
     * 修改
     *
     * @param element 修改元素
     * @param index   位置
     */
    public void update(int element, int index) {
        if (index < 0 || index > this.size) {
            throw new RuntimeException("超出數(shù)組實際的元素范圍!!!");
        }
        data[index] = element;
    }
}

注意事項

數(shù)組是一個連續(xù)的內(nèi)存空間,因此支持隨機訪問,即可以通過下標(biāo)快速訪問數(shù)組中的任意位置的元素。

下標(biāo)是從0開始,介于[0,x) 之間,下標(biāo)不能大于等于x,否則就會報數(shù)組下標(biāo)越界異常。

數(shù)組是引用類型,當(dāng)我們創(chuàng)建一個數(shù)組后,會在堆中分配一塊內(nèi)存同時棧中指向堆中內(nèi)存。

責(zé)任編輯:武曉燕 來源: java從零到壹
相關(guān)推薦

2023-03-28 07:44:23

數(shù)據(jù)結(jié)構(gòu)數(shù)組

2021-07-14 23:55:18

數(shù)據(jù)結(jié)構(gòu)數(shù)組

2023-06-07 15:25:19

Kafka版本日志

2025-01-07 08:00:00

有序集合數(shù)據(jù)結(jié)構(gòu)

2016-08-24 20:09:27

Linux數(shù)據(jù)結(jié)構(gòu)位數(shù)組

2016-11-14 10:17:41

2020-09-28 08:11:14

JavaScript數(shù)據(jù)

2010-03-16 16:47:25

Python數(shù)組

2024-01-15 06:01:36

C++數(shù)組

2022-07-28 11:33:23

數(shù)據(jù)分析經(jīng)驗

2021-06-08 06:01:00

C++數(shù)據(jù)結(jié)構(gòu)向量和數(shù)組

2010-07-16 10:38:24

Perl關(guān)聯(lián)數(shù)組

2021-03-08 06:28:57

JAVA數(shù)據(jù)結(jié)構(gòu)與算法稀疏數(shù)組

2011-06-02 09:42:08

數(shù)據(jù)防泄漏數(shù)據(jù)泄漏防護

2019-06-12 22:51:57

Redis軟件開發(fā)

2020-06-29 07:44:36

Redis

2024-01-26 06:42:05

Redis數(shù)據(jù)結(jié)構(gòu)

2022-01-18 19:13:52

背包問題數(shù)據(jù)結(jié)構(gòu)算法

2021-04-25 14:29:02

數(shù)據(jù)結(jié)構(gòu)動態(tài)數(shù)組時間復(fù)雜度

2024-09-28 10:50:08

數(shù)據(jù)飛輪數(shù)據(jù)中臺數(shù)據(jù)技術(shù)
點贊
收藏

51CTO技術(shù)棧公眾號