概述
数组类似一种集合或列表,比如购物清单,我们看到的返回的一条条数据,都称之为数组,数组在内存中是一块连续的空间,可以保存相同类型的多个数据的容器、当然其中包括一维数组,二维数组,n维数组,但是一般常用的也就前面两个。
特征
数组包含列表的特征,同时具有自己独有的特征,数组中存在一个重要的概念称之为索引。
首先数组会有数字构成的索引来表示元素所在的位置,索引的位置是从0开始的,我们可以根据索引来快速访问数组的元素。
图片
数组当中的元素在内存中是连续存储的,且每个元素占用相同的内存、数组是有序的、数组的长度一旦确定是不可变的、当然数组也可以通过System.arraycopy方法进行数组的替换,简单理解就是扩容。
模拟数组的基本操作
其中包括添加,修改,删除,遍历,扩容等操作
package com.structure.array;
/**
* Created by IDEA
*
* @author:m1397
* @Time:2022/4/4 - 7:11
* 数组:是有限个类型相同的变量所组成的有序集合,元素与元素之间紧密排列,既不能打乱元素的存储顺序,也不能跳过某个单元进行存储
*/
public class ArrayDemo {
/**
* 数据大小
*/
private int size;
/**
* 数组对象
*/
private int[] data;
/**
* 当前存储数量
*/
private int num;
private ArrayDemo(int size) {
this.size = size;
this.data = new int[size];
this.num = 0;
}
public static void main(String[] args) {
/**
* 数组的基本操作
*/
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("超出数组实际的元素范围!!!");
}
if (this.num >= this.size) {
resize();
}
// 遍历数组,将需要插入的位置之后的元素都往后移动一位
// 首先得移动存在数据的那个位置
// index为下标
for (int i = this.num - 1; i > index - 1; i--) {
data[i + 1] = data[i];
}
data[index] = element;
this.num++;
}
/**
* 扩容
* 将size扩容2倍,复制到新数组里面
*/
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("超出数组实际的元素范围!!!");
}
int deleteElement = data[index];
// 遍历数组,将需要删除的位置之后的元素都往前移动一位
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("超出数组实际的元素范围!!!");
}
data[index] = element;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
注意事项
数组是一个连续的内存空间,因此支持随机访问,即可以通过下标快速访问数组中的任意位置的元素。
下标是从0开始,介于[0,x) 之间,下标不能大于等于x,否则就会报数组下标越界异常。
数组是引用类型,当我们创建一个数组后,会在堆中分配一块内存同时栈中指向堆中内存。